英文:
multiprocessing worker code not executing
问题
I have code like this:
def worker(data_row):
print("In worker", data_row)
if __name__ == '__main__':
db_conn = init_db_conn()
rows = db_conn.execute(query).fetchall()
db_conn.close()
pool = Pool(4)
jobs = []
for row in rows:
print("In main", row)
job = pool.apply_async(worker, (row,))
jobs.append(job)
for job in jobs:
job.get()
pool.join()
pool.close()
When I execute this, only In main
is printed, and zero In worker
, so no worker code is executed. How to resolve this?
英文:
I have code like this:
def worker(data_row):
print("In worker", data_row)
if __name__ == '__main__':
db_conn = init_db_conn()
rows = db_conn.execute(query).fetchall()
db_conn.close()
pool = Pool(4)
jobs = []
for row in rows:
print("In main", row)
job = pool.apply_async(worker, (row,))
jobs.append(job)
for job in jobs:
job.get()
pool.join()
pool.close()
When I execute this, only In main
are printed, and zero In worker
, so no worker code is executed. How to resolve this?
答案1
得分: 0
从文档中可以看出,在使用join
之前,您必须调用close
。
class multiprocessing.pool.Pool
close()
防止提交更多任务给池。一旦所有任务都完成,工作进程将退出。
terminate()
立即停止工作进程,而不完成未完成的工作。在池对象被垃圾回收时,将立即调用terminate()。
join()
等待工作进程退出。在使用join()之前必须调用close()或terminate()。
英文:
from the docs you have to call close
before join
.
class multiprocessing.pool.Pool
close()
Prevents any more tasks from being submitted to the pool.
Once all the tasks have been completed the worker processes will exit.
terminate()
Stops the worker processes immediately without completing
outstanding work. When the pool object is garbage collected
terminate() will be called immediately.
join()
Wait for the worker processes to exit.
One must call close() or terminate() before using join().
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论