Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1206403782 opened this issue Aug 27, 2019 · 0 comments
Open

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

1206403782 opened this issue Aug 27, 2019 · 0 comments

Comments

@1206403782
Copy link

1206403782 commented Aug 27, 2019

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()之后。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant