标签:其他 print pipe 异步调用 程序 补充 pyc 对象 接收
unix开子进程的拷贝一份父进程的数据
进行的三个状态:运行,阻塞,就绪
# 方式1
from multiprocessing import Process
import time
def task(name):
print(‘%s is running‘%name)
time.sleep(5)
print(‘%s is done‘%name)
if __name__ ==‘__main__‘:
p = Process(target=task,args=(‘子进程1‘,))
p.start() #仅仅只是给操作系统发送了一个信号
#方式二:自定义类继承自process
from multiprocessing import Process
import time
class MyProcess(Process):
def __init__(self,name):
super().__init__()
self.name = name
def run(self): #函数名必须是run
print(‘%s is running‘%self.name)
time.sleep(5)
print(‘%s is done‘%self.name)
if __name__ == ‘__main__‘:
p = MyProcess(‘子进程1‘)
p.start()#本质就是在调用p.run
print(‘进‘)
from multiprocessing import Process
import time
import os
def task(name):
print(‘%s is running,parent id is <%s>‘%(os.getpid(),os.getppid()))
time.sleep(5)
print(‘%s is done‘%os.getpid())
if __name__ ==‘__main__‘:
p = Process(target=task,args=(‘子进程1‘,))
p.start() #仅仅只是给操作系统发送了一个信号
print(‘主‘,os.getpid())
#牺牲效率实现子进程串行
from multiprocessing import Process,Lock
import time
def tack(name,lock):
lock.acquire()#加锁
print(‘%s,1‘%name)
time.sleep(1)
print(‘%s,2‘%name)
time.sleep(1)
print(‘%s,3‘%name)
lock.release()#释放锁
if __name__ == ‘__main__‘:
lock = Lock()#实例化锁对象
for i in range(3):
p = Process(target=tack,args=(‘进程%s‘%i,lock))#把锁传到子进程中
p.start()
from multiprocessing import Event
e = Event()# 创建了一事件
print(e.is_set())# 查看一个事件的状态,默认是阻塞
e.set() # 将这个事件状态改为True
print(e.is_set())
e.wait()# 根据e.is_set()的值决定是否阻塞,如果是True就是不阻塞,如果是False就是阻塞状态
e.clear() # 将这个事件状态改为False
import time
import random
from multiprocessing import Process,Event
def light(e):
while True:
if e.is_set():
e.clear()
print(‘红灯亮了‘)
else:
e.set()
print(‘绿灯亮了‘)
time.sleep(5)
def cars(i,e):
if not e.is_set():
print(‘%s 在等待‘%i)
e.wait()
print(‘%s 通行了‘%i)
if __name__ == ‘__main__‘:
e = Event()
p1 = Process(target=light,args=(e,))
p1.start()
for i in range(20):
p = Process(target=cars,args=(i,e))
p.start()
time.sleep(2)
from multiprocessing import Queue
q = Queue(4)#指定队列大小,如果不指定大小则大小为无穷尽
q.put(‘hello‘)#插入数据到队列中
print(q.full())#判断队列中数据是否满了
q.get()#从队列读取并删除一个数据
print(q.empty())#判断队列中数据是否空了
from multiprocessing import Process,Queue
import time
def producer(q):
for i in range(3):
res = ‘包子%s‘%i
time.sleep(0.5)
q.put(res)
print(‘生产者生产了%s‘%res)
def consumer(q):
while True:
res = q.get()
if res == None:break
time.sleep(0.7)
print(‘消费者吃了%s‘%res)
if __name__ == ‘__main__‘:
q = Queue()
p1 = Process(target=producer,args=(q,))
c1 = Process(target=consumer,args=(q,))
p1.start()
c1.start()
p1.join()
c1.join()
q.put(None)
from multiprocessing import Process,JoinableQueue
import time
def producer(q):
for i in range(3):
res = ‘包子%s‘%i
time.sleep(0.5)
q.put(res)
print(‘生产者生产了%s‘%res)
q.join() #阻塞,直到一个队列中的所有数据,全部被处理完毕
def consumer(q):
while True:
res = q.get()
time.sleep(0.7)
print(‘消费者吃了%s‘%res)
q.task_done() # 处理完一个数据,队列中的计数器-1
if __name__ == ‘__main__‘:
q = JoinableQueue()
p1 = Process(target=producer,args=(q,))
c1 = Process(target=consumer,args=(q,))
p1.start()
c1.daemon = True #守护进程,主进程中的代码执行完毕后,子进程自动结束
c1.start()
p1.join()
from multiprocessing import Pipe,Process
def func(conn1,conn2):
conn1.close()
while True:
try:
msg = conn2.recv()
print(msg)
except EOFError:
print(‘子进程结束‘)
conn2.close()
break
if __name__ == ‘__main__‘:
conn1,conn2 = Pipe()
Process(target=func,args=(conn1,conn2)).start()
conn2.close()
for i in range(5):
conn1.send(‘吃屎了你‘)
conn1.close()
from multiprocessing import Pool,Process
import time
import os
def func(i):
print(‘%s 进程正在运行‘%i,os.getpid())
time.sleep(2)
print(‘%s 进程运行结束‘%i,os.getpid())
if __name__ == ‘__main__‘:
pool = Pool(3)
for i in range(10):
# pool.apply(func,args=(i,)) 同步调用
pool.apply_async(func,args=(i,)) # 异步调用
pool.close() # 结束进程池接收任务
pool.join() # 感知进程池中的任务执行结束
# apply_async必须是与close、join一起使用
from multiprocessing import Pool
def func(i):
return i*i
if __name__ == ‘__main__‘:
p = Pool(5)
res_lis = []
for i in range(10):
res = p.apply_async(func,args=(i,))
res_lis.append(res)
for res in res_lis:
print(res.get())# res.get 默认是阻塞的,因为需要接收进程处理的结果,之后接收到结果之后才可以继续执行,所以
# 所以将进程的返回结果放在一个列表中,然后循环这个列表,再执行get就不会阻塞了
p.close()
p.join()
from multiprocessing import Pool
import os
def func1(i):
print(‘in func1‘,os.getpid())
return i*i
def func2(ii):
print(‘in func2‘,os.getpid())
print(ii)
if __name__ == ‘__main__‘:
‘‘‘
1. func1执行结果当作func2的参数,func1进程执行结束后返回结果,通过callback调用func2,将func1的执行返回结果当作参数传递给func2执行
2. func1是一个新的进程
3. func2是在主进程中执行的
‘‘‘
p = Pool(5)
p.apply_async(func1,args=(2,),callback=func2)
p.close()
p.join()
标签:其他 print pipe 异步调用 程序 补充 pyc 对象 接收
原文地址:https://www.cnblogs.com/wualin/p/9956900.html