Skip to content

2.3 多进程编程的Pool.py 是否缺了主函数? #18

Description

@1206403782

from multiprocessing import Process,Pool
import time

def Foo(i):
time.sleep(2)
return i+100

def Bar(arg):
print('-->exec done:',arg)

if name == "main":
pool = Pool(5) #允许进程池同时放入5个进程

    for i in range(10):
            pool.apply_async(func=Foo, args=(i,),callback=Bar)  #func子进程执行完后,才会执行callback,否则callback不执行(而且callback是由父进程来执行了)
    
    print('end')
    pool.close()
    pool.join() #主进程等待所有子进程执行完毕。必须在close()或terminate()之后。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions