如何退出Python Multiprocessing循环?

时间:2011-03-17 19:21:51

标签: python multiprocessing

我有一个多处理代码,可以搜索特定条件的数据库记录,当它到达数据库的末尾时,循环必须停止,我该怎么做?这是代码:

import psycopg2
from multiprocessing import Pool

conn = psycopg2.connect(a database)
query=conn.cursor()
query.execute(some query)

def scanfile():
    try: row=query.fetchone()
    except: return False #here the loop must stop 
    #do something with row...

if __name__=='__main__':
    pool=Pool()
    while True:
        pool.apply_async(scanfile)
    pool.close()
    pool.join()

1 个答案:

答案 0 :(得分:1)

将您的查询/光标移动到iterable上,让多处理为您分配工作。

import psycopg2
from multiprocessing import Pool


def scanfile(row):
    #do something with row...


if __name__ == '__main__':
    conn = psycopg2.connect(a database)
    query = conn.cursor()
    query.execute(some query)
    pool = Pool()
    pool.map_async(scanfile, query).wait()
    pool.close()
    pool.join()