python异步

时间:2011-09-05 18:43:41

标签: python

想象一下20 mb的文本文件。我正在通过char读取char并提取有用的信息。我实际上有两个主要功能,一个是读取文件,另一个是提取信息。像这样:

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

现在,我的目标是在提取器正常工作时继续阅读。基本上,我的问题是实现目标的适当方法是什么?

1 个答案:

答案 0 :(得分: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)

正如您将看到的那样,如果执行此操作,将按正确的顺序使用所有内容。