2011-09-05 37 views
2

想象一下20 MB的文本文件。我正在通过字符读取字符并提取有用的信息。实际上我有两个主要功能,一个是读取文件,另一个是提取信息。类似这样的:python不同步

def reader(path): 
    f = open(path, 'r') 
    source = f.read() 
    f.close() 

    while True: 
     # here is where I read char by char and call the function extractor 

def extractor(s): 
    # here I extract the useful information 

现在,我的目标是在提取器工作时继续阅读。所以基本上,我的问题是什么才能实现我的目标?

+0

哪个版本的Python?因为在3.2+,我推荐concurrent.futures模块。 – utdemir

+0

我正在使用Python 2.7 – Shaokan

+1

您是否确实看到性能问题而没有并发读取和处理?从现代硬盘读取20MB应该只需几秒钟,因此获得这个时间是潜在加速的绝对限制。 –

回答

3

您可以使用生产者/消费者线程。线程可以使用Queue.Queue进行同步。

编辑:生产者/消费者系统的一个例子:

from threading import Thread 
from Queue import Queue 


def produce(queue, n_items): 
    for d in range(n_items): 
     queue.put(d) 
     print "put {0} in queue".format(d) 

def consume(queue, n_items): 
    d = 0 
    while d != n_items -1: # You need some sort of stop condition 
     d = queue.get() 
     print "got {0} from queue".format(d) 

def start_producer_and_consumer(wait): 
    q = Queue() 
    consumer_thread = Thread(target = consume, args = (q, 10)) 
    producer_thread = Thread(target = produce, args = (q, 10)) 
    producer_thread.start() 
    consumer_thread.start() 
    if wait: 
     producer_thread.join() 
     consumer_thread.join() 

if __name__ == '__main__': 
    start_producer_and_consumer(True) 

至于如果执行此,你会看到,一切都会以正确的顺序被消耗。

+0

我有线程问题。例如,如果我使用线程将1,2,3,4,5,6,7,8,9放入队列中,我奇怪地收到一个结果,如1,3,4,5,2,6,8,7 ,9 – Shaokan

+0

编辑我的答案来解决这个问题。 – pvoosten