标签:ret one callback art from free 串行 join() 进程锁
一、进程锁
from multiprocessing import Process,Lock
def lock_f(l,i):
l.acquire()
try:
print ("multip - %s" % i )
finally:
l.release()
if __name__=="__main__":
lock = Lock()
for num in range(10):
Process(target=lock_f,args = (lock,num)).start()
二、进程池
from multiprocessing import Process,Pool,freeze_support
import time
import os
def Foo(i):
time.sleep(1)
print("in process!",os.getpid())
return i+100
def Bar(args):
print ("-->exec done!",args,os.getpid())
if __name__=="__main__":
pool = Pool(processes=2) #允许进程池同时放入2个进程;
print("主进程池:",os.getpid())
for i in range(10):
#pool.apply(func = Foo,args=(i,))#apply支持串行;
#pool.apply_async(func=Foo,args=(i,)) #apply_async支持并行;
pool.apply_async(func=Foo,args=(i,),callback=Bar)
print ("end!")
pool.close()
pool.join()#进程池中进程,执行完毕后,再关闭;如果注视掉该行,那么程序直接关闭.
标签:ret one callback art from free 串行 join() 进程锁
原文地址:http://www.cnblogs.com/wulafuer/p/7953841.html