python进程池
import os
from multiprocessing import Pool
import time
a = 1
def task(name):
global a
for i in range(3):
time.sleep(1)
print(f"这是任务{name},进程id为:{os.getpid()},a的值为{a}")
return f"这是任务{name},进程id为:{os.getpid()},a的值为{a}"
b = []
def fun(n):
b.append(n)
print("这是b",b)
if __name__ == '__main__':
pool = Pool(3)
for i in range(5):
pool.apply_async(func=task, args=(i,), callback=fun)
pool.close()
pool.join()
print(a)
print(b)
print("over")