1  队列读写

# -*- coding: utf-8 -*-
"""
多进程 共享 队列 multiprocessing.Process
逻辑:
一个进程往队列写数据,一个进程从读写读数据
写进程完了后,主进程强行结束读进程 使用:
1. 创建队列 q = multiprocessing.Queue() ,默认无限大小,可以指定大小
2. 把队列 q 当参数传给 子进程 执行代码, 猜测应该不能通过全局变量的方式访问
3. 在子进程中读写队列数据 q.put(<data>) q.get() 参考:
方法
'cancel_join_thread', 'close', 'empty', 'full', 'get', 'get_nowait', 'join_thread', 'put', 'put_nowait', 'qsize' """ from multiprocessing import Queue, Process
import time def write(q):
for i in ['a','b','c','d']:
time.sleep(2)
q.put(i)
print ('put {0} to queue'.format(i)) def read(q):
while 1:
time.sleep(2)
result = q.get()
print ("get {0} from queue".format(result)) def main():
q = Queue() pw = Process(target=write, args=(q,))
pr = Process(target=read, args=(q,))
pw.start()
pr.start()
pw.join()
pr.terminate() # 强行终止读进程 if __name__ == '__main__':
main() """
Out: put a to queue
get a from queue
put b to queue
get b from queue
put c to queue
get c from queue
put d to queue
get d from queue
"""

2 队列实现生产者、消费者

# -*- coding: utf-8 -*-
"""
多进程 生产者 消费者模型,使用队列实现 multiprocessing.Queue 逻辑:
1个生产者,1个消费者在2个不同的进程中操作同一个队列
生产者的速度是消费者的3倍
"""
import multiprocessing
import random
import time # 生产者
class producer(multiprocessing.Process):
def __init__(self, queue):
multiprocessing.Process.__init__(self) # 父类构造
self.queue = queue def run(self):
for i in range(10):
item = random.randint(0, 256) # 往队列写数据
self.queue.put(item) print("Process Producer: item %d appended to queue %s " \
%(item, self.name))
time.sleep(1)
print("The size of queue is %s" \
% self.queue.qsize()) # 消费者
class consumer(multiprocessing.Process):
def __init__(self, queue):
multiprocessing.Process.__init__(self) # 父类构造
self.queue = queue def run(self):
while True:
if (self.queue.empty()):
print("the queue is empty")
break
else:
time.sleep(2) # 从队列读取数据,队列为空会阻塞,这做了非空判断,只有一个进程读,不会阻塞
item = self.queue.get() print("Process Consumer: item %d poped from by %s " \
% (item, self.name))
time.sleep(1) if __name__ == '__main__':
# 多进程共享对列
queue = multiprocessing.Queue() ## 启动生产者、消费者
process_producer = producer(queue)
process_consumer = consumer(queue)
process_producer.start()
process_consumer.start()
process_producer.join()
process_consumer.join() """
Out:
the queue is empty
Process Producer: item 225 appended to queue producer-1
The size of queue is 1
Process Producer: item 101 appended to queue producer-1
The size of queue is 2
Process Producer: item 50 appended to queue producer-1
The size of queue is 3
Process Producer: item 217 appended to queue producer-1
The size of queue is 4
Process Producer: item 75 appended to queue producer-1
The size of queue is 5
Process Producer: item 45 appended to queue producer-1
The size of queue is 6
Process Producer: item 19 appended to queue producer-1
The size of queue is 7
Process Producer: item 157 appended to queue producer-1
The size of queue is 8
Process Producer: item 127 appended to queue producer-1
The size of queue is 9
Process Producer: item 223 appended to queue producer-1
The size of queue is 10
"""

最新文章

  1. ceph hadoop spark 大数据处理
  2. shell中的条件判断、参数以及变量替换
  3. Spring中的JDBCTemplate
  4. ROM、RAM、DRAM、SRAM和FLASH区别
  5. Android viewPage notifyDataSetChanged无刷新
  6. hdu 4658 Integer Partition
  7. USB中CDC-ECM的了解和配置
  8. jQuery 操作 input 之 checkbox
  9. javaWeb学习总结(7)- 使用Session防止表单重复提交
  10. 学习笔记 intent属性
  11. logback常用配置详解及logback简介
  12. Beamer 目录分栏
  13. Hillstone目的地址转换DNAT配置
  14. BAT:通过连接符处理判断OR的关系
  15. ASP.NET MVC + EF 更新的几种方式
  16. ODOO区分测试库和正式库的简单方法
  17. 5C - A == B ?
  18. Oracle GI 日志收集工具 - TFA
  19. dyld: Library not loaded: @rpath/XCTest.framework/XCTest Referenced from: /private/var/mobile/Conta
  20. C# 获取指定目录下所有文件信息

热门文章

  1. Flask 教程 第六章:个人主页和头像
  2. python高级编程——进程和进程池
  3. Cobalt Strike系列教程第四章:文件/进程管理与键盘记录
  4. Mysql存储过程--大于十分钟执行
  5. IOR and mdtest - measure parallel file system I/O performance at both the POSIX and MPI-IO level.
  6. Linux-3.14.12内存管理笔记【构建内存管理框架(2)】
  7. 通过JTAG对比内核启动后text/rodata段内容
  8. SPA项目开发之动态树以及数据表格和分页
  9. vue 借用element-ui实现头像上传 axios发送请求
  10. 201777010217-金云馨《面向对象程序设计(Java)》第二周学习总结