2014-01-28 29 views
0

我已经编写了读取远程系统文件的代码,这些文件工作正常,但读取远程文件需要很长时间。我想提高阅读的表现。如何在python中提高读取远程系统文件的性能

我也用python线程来读取远程系统文件。这也需要很多时间。现在任何人都可以让我知道更好的建议。

我已经使用这个代码,用于读取远程系统文件,

root_folder="\\\\192.168.1.1\\C$" 
    try: 
     use_dict={} 
     use_dict['remote']=unicode(root_folder) 
     use_dict['password']=unicode("example") 
     use_dict['username']=unicode("example") 
     win32net.NetUseAdd(None, 2, use_dict) 
     print "Network connection established" 
    except: 
     print "Network connection failed" 

    for root, dirnames, filenames in os.walk(root_folder): 
     for filename in filenames: 
      match=os.path.join(root, filename) 
      datafile = file(match) 
      for line in datafile: 
       for li in line: 
        print li 

从这个代码,45分钟时间正在读取远程系统文件。如果我以本地方式读取相同的文件,那么它只需要5分钟。所以我无法提高阅读效果。请让我知道了提高性能的阅读...

感谢...

回答

0

你可以尝试多处理。在这个例子中,一个进程从网络中读取数据,另一个进程打印数据,并通过队列连接。

from multiprocessing import Process, Queue 

def readfiles(q): 

    root_folder="\\\\192.168.1.1\\C$" 
    try: 
     use_dict={} 
     use_dict['remote']=unicode(root_folder) 
     use_dict['password']=unicode("example") 
     use_dict['username']=unicode("example") 
     win32net.NetUseAdd(None, 2, use_dict) 
     print "Network connection established" 
    except: 
     print "Network connection failed" 

    for root, dirnames, filenames in os.walk(root_folder): 
     for filename in filenames: 
      match=os.path.join(root, filename) 
      datafile = file(match) 
      for line in datafile: 
       q.put(line) 
    q.close() 


if __name__ == '__main__': 
    q = Queue() 
    p = Process(target=readfiles, args=(q,)) 
    p.start() 

    while p.is_alive() or not q.empty(): 
     for li in q.get(): 
      print li 
相关问题