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
"""

[b0038] python 归纳 (二三)_多进程数据共享和同步_队列Queue的更多相关文章

  1. [b0037] python 归纳 (二二)_多进程数据共享和同步_管道Pipe

    # -*- coding: utf-8 -*- """ 多进程数据共享 管道Pipe 逻辑: 2个进程,各自发送数据到管道,对方从管道中取到数据 总结: 1.只适合两个进 ...

  2. [b0036] python 归纳 (二一)_多进程数据共享和同步_服务进程Manager

    # -*- coding: utf-8 -*- """ 多进程数据共享 服务器进程 multiprocessing.Manager 入门使用 逻辑: 20个子线程修改共享 ...

  3. [b0035] python 归纳 (二十)_多进程数据共享和同步_共享内存Value & Array

    1. Code # -*- coding: utf-8 -*- """ 多进程 数据共享 共享变量 Value,Array 逻辑: 2个进程,对同一份数据,一个做加法,一 ...

  4. [b0041] python 归纳 (二六)_多进程数据共享和同步_事件Event

    # -*- coding: utf-8 -*- """ 多进程 同步 事件multiprocessing.Event 逻辑: 子线程负责打印,会阻塞, 等待主进程发出控制 ...

  5. [b0040] python 归纳 (二五)_多进程数据共享和同步_信号量Semaphore

    # -*- coding: utf-8 -*- """ 多进程同步 使用信号量 multiprocessing.Semaphore 逻辑: 启动5个进程,打印,每个各自睡 ...

  6. [b0039] python 归纳 (二四)_多进程数据共享和同步_锁Lock&RLock

    # -*- coding: utf-8 -*- """ 多进程 锁使用 逻辑: 10个进程各种睡眠2秒,然后打印. 不加锁同时打印出来,总共2秒,加锁一个接一个打印,总共 ...

  7. Python进阶【第二篇】多线程、消息队列queue

    1.Python多线程.多进程 目的提高并发 1.一个应用程序,可以有多进程和多线程 2.默认:单进程,单线程 3.单进程,多线程 IO操作,不占用CPU python的多线程:IO操作,多线程提供并 ...

  8. Python进阶(4)_进程与线程 (python并发编程之多进程)

    一.python并发编程之多进程 1.1 multiprocessing模块介绍 由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大 ...

  9. [b0027] python 归纳 (十二)_并发队列Queue的使用

    # -*- coding: UTF-8 -*- """ 学习队列 Queue 总结: 1. 队列可以设置大小,也可以无限大小 2. 空了,满了,读写时可以阻塞,也可以报错 ...

随机推荐

  1. openstack 搭建

    #所有节点修改ip,主机名和hosts解析 controller 10.0.0.11 controller compute1 10.0.0.31 compute1 #所有节点准备本地repo源 rm ...

  2. golang-错误处理

    1.错误处理 如果要写出健壮 ,易维护的代码 ,错误处理就是关键 ,考虑到可能会发生的意外对其进行处理 go的错误处理与众不同 ,在调用可能出现问题的方法和函数时都会返回一个类型为error的值 ,由 ...

  3. 看完这篇文章,我奶奶都知道什么是JVM中的内存模型与垃圾回收!

    扩展阅读:JVM从入门开始深入每一个底层细节 六.内存模型 6.1.内存模型与运行时数据区 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干不同数据区域. Java内存模型的主要目 ...

  4. datatable与实体类之间相互转化的几种方法

    #region DataTable转换成实体类 /// <summary> /// 填充对象列表:用DataSet的第一个表填充实体类 /// </summary> /// & ...

  5. linux kernel下输入输出console如何实现【转】

    转自:https://blog.csdn.net/skyflying2012/article/details/41078349 最近工作在调试usb虚拟串口,让其作为kernel启动的调试串口,以及u ...

  6. STL 中 map 的使用

    涉及:vector,pair 基本结构 map 从本质上来说是一颗二叉排序树,树上的节点类型为pair.pair类型中有两个重要的成员变量,其中 first 存储了 key ,second 存储了 v ...

  7. 9.jenkins 集群

    一. 集群配置 实际生产中,需要配置集群,来配合使用. jenkins  主节点需要安装插件 SSH Slaves 从节点(就是slave的机器)需要安装 jdk 开发包 yum install -y ...

  8. LeetCode 1244. 力扣排行榜

    地址 https://www.acwing.com/solution/LeetCode/content/5765/ 题目描述新一轮的「力扣杯」编程大赛即将启动,为了动态显示参赛者的得分数据,需要设计一 ...

  9. 算法问题实战策略 SORTGAME

    地址 https://algospot.com/judge/problem/read/SORTGAME 解答 常规BFS是会超时的  按照书上的提示 应该是打表(居然还有提倡打表的题目) tle 代码 ...

  10. 洛谷 P5638 光骓者的荣耀

    洛谷 P5638 [CSGRound2]光骓者的荣耀 洛谷传送门 题目背景 小 K 又在做白日梦了.他进入到他的幻想中,发现他打下了一片江山. 题目描述 小 K 打下的江山一共有nn个城市,城市ii和 ...