【发布时间】:2012-04-24 09:58:00
【问题描述】:
我正在尝试使用多进程池对象。我希望每个进程在启动时打开一个数据库连接,然后使用该连接来处理传入的数据。(而不是为每一位数据打开和关闭连接。)这似乎是初始化程序对于,但我无法理解工作人员和初始化程序是如何通信的。所以我有这样的事情:
def get_cursor():
return psycopg2.connect(...).cursor()
def process_data(data):
# here I'd like to have the cursor so that I can do things with the data
if __name__ == "__main__":
pool = Pool(initializer=get_cursor, initargs=())
pool.map(process_data, get_some_data_iterator())
我如何(或我如何)将光标从 get_cursor() 取回 process_data()?
【问题讨论】: