# -*- encoding: utf-8 -*-from multiprocessing import Process, Queue
import os, time, random
# 写数据进程执行的代码:defwrite(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())# 读数据进程执行的代码:defread(q):print('Process to read: %s'% os.getpid())whileTrue:if q.empty():print('empty')break
value = q.get(True)print('Get %s from queue.'% value)
time.sleep(3)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.join()# pr.terminate() # 强行终止
Process to write: 16940
Put A to queue...
Process to read: 3424
Get A from queue.
Put B to queue...
Put C to queue...
Get B from queue.
Get C from queue.
empty
Process finished with exit code 0
Pool、Queue
# -*- encoding: utf-8 -*-from multiprocessing import Pool, Manager
import os, time, random
classMyPool(object):def__init__(self, count):
self.pool = Pool(count)
self.queue = Manager().Queue()defstart_task(self, task, datas, max_count=3):for i inrange(0,len(datas), max_count):
data = datas[i: i+max_count]
self.pool.apply_async(task, args=(self.queue, data))
self.pool.close()
self.pool.join()defget_result(self):print('=================================')print('Get queue data...')whileTrue:if self.queue.empty():print('empty')break
value = self.queue.get()print('Get %s from queue.'% value)returnTruedeftask(q, datas):'''
写数据进程执行的代码
'''print('Process to write: %s'% os.getpid())for value in datas:print('Put %s to queue...'% value)
q.put(value**2)
time.sleep(random.random())defmain():print('Parent process %s.'% os.getpid())
datas =[1,2,3,4,5,6,7,8,9,10]
count =3# 进程数
my_pool_obj = MyPool(count)
my_pool_obj.start_task(task, datas)
my_pool_obj.get_result()returnTrueif __name__ =='__main__':
main()
Parent process 260.
Process to write: 14440
Put 7 to queue...
Process to write: 7644
Put 4 to queue...
Process to write: 7208
Put 1 to queue...
Put 2 to queue...
Put 5 to queue...
Put 8 to queue...
Put 3 to queue...
Put 6 to queue...
Process to write: 7208
Put 10 to queue...
Put 9 to queue...
=================================
Get queue data...
Get 49 from queue.
Get 16 from queue.
Get 1 from queue.
Get 4 from queue.
Get 25 from queue.
Get 64 from queue.
Get 9 from queue.
Get 36 from queue.
Get 100 from queue.
Get 81 from queue.
empty
Process finished with exit code 0