多个进程间通信之Queue
多个进程间通信之Queue
实现数据传递
#!coding:utf-8
from multiprocessing import Process, Queue
import os,time,random
#写数据进程执行的代码
def proc_write(q,urls):
print 'Process is write....'
for url in urls:
q.put(url)
print 'put %s to queue... ' %url
time.sleep(random.random())
#读数据进程的代码
def proc_read(q):
print('Process is reading...')
while True:
url = q.get(True)
print('Get %s from queue' %url)
if __name__ == '__main__':
#父进程创建Queue,并传给各个子进程
q = Queue()
proc_write1 = Process(target=proc_write,args=(q,['url_1','url_2','url_3']))
proc_write2 = Process(target=proc_write,args=(q,['url_4','url_5','url_6']))
proc_reader = Process(target=proc_read,args=(q,))
#启动子进程,写入
proc_write1.start()
proc_write2.start()
proc_reader.start()
#等待proc_write1结束
proc_write1.join()
proc_write2.join()
#proc_raader进程是死循环,强制结束
proc_reader.terminate()
https://www.cnblogs.com/guguobao/p/9398653.html
内置模块queue
import queue
线程安全的队列,适用于多线程编程的先进先出的数据结构
- 队列 FIFO Queue
- 栈 LIFO LifoQueue
- 优先级队列 PriorityQueue