标签:reading 交换 scom join cat sleep read ipc get
import time
import multiprocessing
class MulProcess(multiprocessing.Process):
def __init__(self,arg):
super().__init__()
self.arg = arg
def run(self):
while True:
print(time.ctime())
time.sleep(self.arg)
if __name__ == "__main__":
p = MulProcess(2)
p.start()
import time
import multiprocessing
def scz(l, q):
for i in l:
#放入产品
q.put(i)
print("放入产品:" , i , time.ctime())
def xfz(q):
while True:
print("消费产品:" , q.get() , time.ctime())
#发出信号通知任务完成
q.task_done()
if __name__ == "__main__":
#实例化进程队列
q = multiprocessing.JoinableQueue()
#实例化线程
p = multiprocessing.Process(target=xfz, args=(q,))
#守护进程,主进程结束,子进程也跟着结束
p.daemon = True
p.start()
l = [i for i in range(1,5)]
scz(l,q)
#等待队列中所有项都处理完成
q.join()
import time
import multiprocessing
def scz(l, q):
for i in l:
q.put(i)
print("放入产品:" , i , time.ctime())
def xfz(q):
while True:
a = q.get()
if a is None:
break
else:
print("消费产品:" , a , time.ctime())
if __name__ == "__main__":
q = multiprocessing.JoinableQueue()
p = multiprocessing.Process(target=xfz, args=(q,))
p.start()
p1 = multiprocessing.Process(target=xfz, args=(q,))
p1.start()
l = [i for i in range(1,5)]
scz(l,q)
q.put(None)
q1.put(None)
q.join()
q.join()
标签:reading 交换 scom join cat sleep read ipc get
原文地址:https://www.cnblogs.com/TK-tank/p/12312949.html