#- * -coding: utf - 8 - * -
from multiprocessing
import Process, Queue
import os
import time
import random
# 写数据进程执行的代码:
def write(q):
print('Process to write: %s' % os.getpid())
for value in ['A', 'B', 'C']:
print('Put %s to queue...' % value)
q.put(value)
time.sleep(random.random())
# 读数据进程执行的代码:
def read(q):
print('Process to read: %s' % os.getpid())
while True:
value = q.get()
print('Get %s from queue.' % value)
if __name__ == '__main__': #父进程创建Queue, 并传给各个子进程:
q = Queue()
pw = Process(target = write, args = (q, ))
pr = Process(target = read, args = (q, ))# 启动子进程pw, 写入:
pw.start()# 启动子进程pr, 读取:
pr.start()# 等待pw结束:
pw.join()# pr进程里是死循环, 无法等待其结束, 只能强行终止:
pr.terminate()
Process to read: 5836 Process to write: 6472 Put A to queue... Put B to queue... Get A from queue. Put C to queue... Get B from queue. Get C from queue. Process finished with exit code 0
#- * -coding: utf - 8 - * -
from multiprocessing
import Process, Pipe
def f(conn):
conn.send([42, None, 'hello'])
while True:
print(conn.recv())
if __name__ == '__main__':
parent_conn, child_conn = Pipe()
p = Process(target = f, args = (child_conn, ))
p.start()
print parent_conn.recv()# prints "[42, None, 'hello']"
parent_conn.send('666')
p.terminate()
[42, None, 'hello'] 666 Process finished with exit code 0
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有