process in python

from multiprocessing import Process,Pool #进程池
import os,time
def run(i):
    time.sleep(1)
    print ("in the process",os.getpid())
    return i+100
def bar(arg):
    print ('bar is ',os.getpid())
if __name__=='__main__':
    p=Pool(5)
    for i in range(10):
        p.apply_async(func =run,args=(i,),callback= bar)#apply is a serial execution process apply_async is a parallel execution process. The callback is a callback method, which is called when a process ends and is used for some follow-up work. Such as writing to the database log, etc.
    p.close()
    p.join() #Must be close first, then join.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325184994&siteId=291194637