Python随心记--进程调用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python随心记--进程调用相关的知识,希望对你有一定的参考价值。
进程调用
from multiprocessing import Process import time def f(name): time.sleep(1) print(‘hello‘,name,time.ctime()) if __name__ == ‘__main__‘: p_list = [] for i in range(3): p = Process(target=f,args=(‘alive‘,)) p_list.append(p) p.start() for i in p_list: p.join() print(‘end‘)
from multiprocessing import Process import time class MyProcess(Process): # def __init__(self): # super(MyProcess,self).__init__() def run(self): time.sleep(1) print(‘hello‘,self.name,time.ctime()) if __name__ == ‘__main__‘: p_list = [] for i in range(3): p = MyProcess() p.daemon = True #守护进程 p.start() p_list.append(p) # for i in p_list: # p.join() print(‘end‘)
from multiprocessing import Process import time,os def info(title): print(‘title:‘,title) print(‘parent process:‘,os.getppid()) print(‘process id:‘,os.getpid()) def f(name): info(‘function f‘) print(‘hello‘,name) if __name__ == ‘__main__‘: info(‘main process line‘) time.sleep(1) print(‘---------------‘) p = Process(target=info,args=(‘lin‘,)) p.start() p.join() #is_alive from multiprocessing import Process import time class MyProcess(Process): def __init__(self,num): super(MyProcess,self).__init__() self.num = num def run(self): time.sleep(2) print(self.pid) print(self.is_alive()) print(self.num) if __name__ == ‘__main__‘: p_list = [] for i in range(10): p = MyProcess(i) p_list.append(p) for i in p_list: i.start()
进程通讯
Process([group[,target[,name[,args[,kwargs]]]]])
group:线程组,目前还没实现,库引用中提示必须是None
target:要执行的方法
name:进程名
args/kwargs:传入的参数
实例方法
#is_alive():返回进程是否在进行
#join() 阻塞
#start() 开启
#run() start()调用run方法
#terminate() 终止任务
属性
daemon
......
队列通讯
import multiprocessing import queue,time def foo(q): time.sleep(1) print(q(id)) q.put(13) q.put(‘achun‘) if __name__ == ‘__main__‘: # q = queue.Queue() q = multiprocessing.Queue() p = multiprocessing.Process(target=foo,args=(q,)) p.start() p.join() print(q.get()) print(q.get())
管道通信
from multiprocessing import Process,Pipe def f(conn): conn.send([112,{‘name‘:‘lin‘},‘hello‘]) respones = conn.recv() print(‘respones‘,respones) conn.close() print(‘q_ID2:‘,id(conn)) if __name__ == ‘__main__‘: parent_conn,child_conn = Pipe() #双向管道 print(‘q_ID2:‘, id(child_conn)) p = Process(target=f,args=(child_conn,)) p.start() print(parent_conn.recv()) parent_conn.send(‘hello,hao are you‘) p.join()
进程通信 资源(数据)共享
from multiprocessing import Process,Manager def f(d,l,n): d[n] = ‘1‘ d[‘2‘] = 2 d[0,25] = None l.append(n) if __name__ == ‘__main__‘: with Manager() as manager: d = manager.dict() l = manager.list(range(5)) print(‘main process:‘,id(d),id(l)) p_list = [] for i in range(10): p = Process(target=f,args=(d,l,i)) p.start() p_list.append(p) for res in p_list: res.join() print(d) print(l)
进程同步
from multiprocessing import Process,Lock def f(l,i): with l: print(‘hello world %s ‘ %i) if __name__ == ‘__main__‘: lock = Lock() for num in range(10): Process(target=f,args=(lock,num)).start()
进程池
from multiprocessing import Process,Pool import time,os def Foo(i): time.sleep(1) print(i) return i+100 def Bar(arg): print(os.getpid()) print(os.getppid()) print(‘logger:‘,arg) # Bar(1) if __name__ == ‘__main__‘: pool = Pool(5) # 创建进程池对象(限制为5个) for i in range(100): # pool.apply(func = Foo,args = (i,)) #同步 pool.apply_async(func = Foo,args = (i,),callback = Bar) #异步 pool.close() pool.join() print(‘ending...........‘)
协程:协作式(协程是非抢占式的程序)----(生成器)(yield(协程))
#优势1没有切换的消耗
# 2没有锁的概念
import time,queue def consumer(name): print(‘.................‘) while True: new_baozi = yield print(‘[%s] is eating baozi %s‘ %(name,new_baozi)) def product(): r = con.__next__() r = con2.__next__() n = 0 while 1: time.sleep(1) print(‘