multiprocessing worker code not executing

huangapple go评论75阅读模式
英文:

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().

huangapple
  • 本文由 发表于 2023年6月29日 06:43:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定