队列,event,multiprocess
队列:queue
queue is especially useful in threaded programming when information must be exchanged safely between multiple threads.
有三种队列模式
- class
queue.
Queue
(maxsize=0) #先入先出
- class
queue.
LifoQueue
(maxsize=0) #last in fisrt out
- class
queue.
PriorityQueue
(maxsize=0) #存储数据时可设置优先级的队列
用法
>>> import queue
>>> q=queue.Queue(maxsize=0) #如果maxsize小于0或者等于0表示队列无穷大
>>> q.qsize()
0
>>> q.put("")
>>> q.qsize() #获得几个槽被占用了
1
>>> q.full() #如果队列的槽没有可利用的,则返回True
False
>>> q.empty()
False
>>> q.get() # 将queue中的item取出来
''
>>> q.empty() #如果队列的槽里没有item,则返回True
True
>>>
q.put(block=False) 相当于q.put_nowait()
q.get(block=False) 相当于q.get_nowait()
q.join()
Blocks until all items in the queue have been gotten and processed
q.task_done()
Indicate that a formerly enqueued task is complete. Used by queue consumer threads.
消费者生产者模式
在并发编程中使用生产者和消费者模式能够解决绝大多数并发问题。该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度。
为什么要使用生产者和消费者模式
在线程世界里,生产者就是生产数据的线程,消费者就是消费数据的线程。在多线程开发当中,如果生产者处理速度很快,而消费者处理速度很慢,那么生产者就必须等待消费者处理完,才能继续生产数据。同样的道理,如果消费者的处理能力大于生产者,那么消费者就必须等待生产者。为了解决这个问题于是引入了生产者和消费者模式。
什么是生产者消费者模式
生产者消费者模式是通过一个容器来解决生产者和消费者的强耦合问题。生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯,所以生产者生产完数据之后不用等待消费者处理,直接扔给阻塞队列,消费者不找生产者要数据,而是直接从阻塞队列里取,阻塞队列就相当于一个缓冲区,平衡了生产者和消费者的处理能力。
import queue
import threading
q=queue.Queue(maxsize =0) #maxsize设置这个队列一共有无穷大
def producer():
for i in range(10):
q.put("骨头%s" %i) #put后面可以接收很多基本的数据类型
print("开始等待所有的骨头被领走")
q.join() #阻塞,当get任务全部被完成后才执行join后面的语句
print("骨头被领完了")
def consumer(name):
while not q.empty(): #如果槽里有东西
print("%s 吃了%s" %(name,q.get()))
q.task_done()#告知任务完成,每一个get后面都需要加一个这个,如果程序里有join
t= threading.Thread(target=producer) #获得一个线程对象
t.start() #启动线程
consumer('egon')
吃包子的例子
import random,queue,threading
import time
q=queue.Queue()
def producer(name):
count = 0
while count < 20:
if q.qsize() < 8:
time.sleep(random.randrange(2))
q.put(count)
print("%s have make %s baozi" %(name,count))
else:
print("baozi remain more than 8")
count+=1
def consumer(name):
count =0
while count < 8:
time.sleep(random.randrange(3))
if not q.empty():
data = q.get()
time.sleep(0.5)
print('\033[32;1mConsumer %s has eat %s baozi...\033[0m' % (name, data))
else:
print("no more baozi to eat")
count+=1
t1=threading.Thread(target=producer,args=("lidachu1",))
t2=threading.Thread(target=producer,args=("lidachu2",))
c1=threading.Thread(target=consumer,args=("yuyang",))
c2=threading.Thread(target=consumer,args=("xiaowang",))
c3=threading.Thread(target=consumer,args=("shuaibai",))
t1.start()
t2.start()
c1.start()
c2.start()
c3.start()
t1.join()
t2.join()
c1.join()
c2.join()
c3.join()
print(q.qsize())
多进程multiprocess
multiprocessing
is a package that supports spawning processes using an API similar to the threading
module. The multiprocessing
package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing
module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.
multiprocess 是一个能支持产生多个进程的包,使用一个与threading模块类似的API(application programming interface)接口。这个multiprocessing 包能够提供本地和远程的并发(并行)),有效的回避全局解释器锁通过使用子进程而不是线程。由于这个,这个multiprocessing 模块允许程序员充分利用给定机器上的多个处理器。它可以运行在UNIX和WIndows上。
multiprocess模块与threading模块的用法类似
import time
import multiprocessing
def add():
sum = 1
for i in range(2,100000):
sum*=i
print("sum:",sum)
def multi():
count = 2
for i in range(21):
count=count**2
print("count:",count)
# s=time.time()
# add()
# multi()
# print(time.time()-s)
if __name__ == '__main__':
p1=multiprocessing.Process(target=add)
p2=multiprocessing.Process(target=multi)
s=time.time()
p1.start()
p2.start()
p1.join()
p2.join()
print(time.time()-s)
如果使用顺序执行这两个函数所需要的时间大致是20秒左右,如果使用多进程需要的时间是不到12秒。可以看出多进程的优势,前提是这两个函数的运行时间都差不多相同。
event事件
event=threading.Event() 用来进行两个或多个线程之间的交互
官方链接: https://docs.python.org/2/library/threading.html#event-objects
This is one of the simplest mechanisms for communication between threads: one thread signals an event and other threads wait for it.
线程之间一种最简单的交流机制是:一个线程指示一个事件,并且其他线程等待它。
An event is a simple synchronization object; event是一个简单的同步对象
the event represents an internal flag, and threads event代表了内部的标志位和线程
can wait for the flag to be set, or set or clear the flag themselves. 能等待标志位被设置或者清除这个flag
event = threading.Event()
# a client thread can wait for the flag to be set
event.wait()
# a server thread can set or reset it
If the flag is set, the wait method doesn’t do anything. 如果flag被设置,wait方法不会起任何作用
If the flag is cleared, wait will block until it becomes set again. 如果flag为FALSE,wait方法会阻塞,直到flag设置为True。
Any number of threads may wait for the same event 任何数量的线程可能等待这相同的event
在初始化时,默认的标志位为False,
event.set() # 将flag设置为True
event.clear() #将flag设置为False
event.is_set() #返回一个bool 值,判断标志位的状态
event.wait()
If the internal flag is true on entry, return immediately. Otherwise, block until another thread calls set()
to set the flag to true, or until the optional timeout occurs.
#会一直阻塞直到内部的flag 为True,如果内部的标志位在入口时是True,会立刻返回,否则,会一直阻塞,直到另一个线程调用set()方法,设置flag 为True,或者知道可选的超时时间发生。
import threading
import time,random
event=threading.Event()
def foo():
event.wait()
time.sleep(1)
print("first>>>")
def bar():
sum = 0
for i in range (10):
sum+=i
print(sum)
event.set()
t1=threading.Thread(target=foo)
t2=threading.Thread(target=bar)
t1.start()
t2.start()
红绿灯
# 2、设计一个关于红绿灯的线程,5个关于车的线程;
#
# 对于车线程,每隔一个随机秒数,判断红绿灯的状态,是红灯或者黄灯,打印waiting;是绿灯打印running。
#
# 对于红绿灯线程:
# 首先默认是绿灯,做一个计数器,十秒前,每隔一秒打印“light green”;第十秒到第十三秒,每隔一秒打印“light yellow”,13秒到20秒,
# ‘light red’,20秒以后计数器清零。重新循环。
#
# 知识点:event对象(提示:event对象即红绿灯,为true是即绿灯,false时为黄灯或者红
import threading
import time,random
def car(name):
while True:
time.sleep(random.randrange(20))
if event.is_set():
print('\033[41;1m--%s running---\033[0m' %name)
elif not event.is_set():
print('\033[45;1m--%s waiting---\033[0m' %name)
def light():
count = 0
event.set()
while True:
if count < 10:
print("----light green----")
time.sleep(1)
count+=1
elif count >= 10 and count < 13:
event.clear()
print("----light yellow----")
time.sleep(1)
count+=1
elif count >= 13 and count < 20:
print("----light red----")
time.sleep(1)
count+=1
elif count == 20:
count =0
event.set() if __name__ == "__main__":
event=threading.Event()
t_light=threading.Thread(target=light)
t_light.start()
for i in range(5):
t_car=threading.Thread(target=car,args=("car%s" %i,))
t_car.start()
主线程 子线程,守护线程
队列,event,multiprocess的更多相关文章
- Python--day38---进程间通信--初识队列(multiprocess.Queue)
初识队列: 进程间通信IPC(Inter-Process Communication) 1,队列的方法: q = Queue(5)1,q.put(1) #把1放进队列 2,print(q.full() ...
- Python--day38---进程间通信--初识队列(multiprocess.Queue)之生产者,消费者模型
1,生产者消费者模型.py import random import time from multiprocessing import Queue, Process def producer(name ...
- 异步、+回调机制、线程queue、线程Event、协程、单线程实现遇到IO切换
# from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor # import requests # import o ...
- python_11(网络编程)
第1章 ucp协议 1.1 特性 1.2 缺陷 1.3 UDP协议实时通信 第2章 socket的更多方法 2.1 面向锁的套接字方法 2.1.1 blocking设置非阻塞 2.1.2 Blocki ...
- Python之进程 进阶 下
在python程序中的进程操作 之前我们已经了解了很多进程相关的理论知识,了解进程是什么应该不再困难了,刚刚我们已经了解了,运行中的程序就是一个进程.所有的进程都是通过它的父进程来创建的.因此,运行起 ...
- 进程、线程、协程的基本解析(python代码)
进程什么是进程?程序就是一堆放在磁盘上的代码,进程是一段程序的运行过程正规点说,进程一般由程序.数据集.进程控制块三部分组成 什么进程切换?进程切换是,一个正在运行的进程被中断,操作系统指定另一个进程 ...
- WeText项目:一个基于.NET实现的DDD、CQRS与微服务架构的演示案例
最近出于工作需要,了解了一下微服务架构(Microservice Architecture,MSA).我经过两周业余时间的努力,凭着自己对微服务架构的理解,从无到有,基于.NET打造了一个演示微服务架 ...
- Python:进程
由于GIL的存在,python一个进程同时只能执行一个线程.因此在python开发时,计算密集型的程序常用多进程,IO密集型的使用多线程 1.多进程创建: #创建方法1:将要执行的方法作为参数传给Pr ...
- 你真的了解setTimeout和setInterval吗?
博客园的代码排版真难用,编辑时候是好的,一保存就是乱了——本文也同时发表在我另一独立博客上 你真的了解setTimeout和setInterval吗?,可以移步至这里吧 setTimeout和setI ...
随机推荐
- 10.29 scrum meeting newbe软件团队工作分配
这次会议,我们主要讨论了目前阶段的主要任务与任务分配问题. 首先,通读代码,理解程序的运行方式是必不可少的环节.所以我们要求团队的所有成员通读代码.并且对于开发人员和测试人员,要求写出我们分配的各自模 ...
- Scrum Meeting 11.04
成员 今日任务 明日计划 用时 徐越 学习Fragment相关知识,代码移植 代码移植 4h 赵庶宏 selvet移植,服务器配置,编写数据库 服务器配置,代码移植 4h 薄霖 学习安卓界面设计数据库 ...
- Java实验五(客户端)
一. 实验内容 1. 运行教材上TCP代码,结对进行,一人服务器,一人客户端: 2. 利用加解密代码包,编译运行代码,客户端加密,服务器解密: 3. 客户端加密明文后将密文通过 ...
- js弹出框 -搜索
警告框alert() alert是警告框,只有一个按钮“确定”无返回值,警告框经常用于确保用户可以得到某些信息.当警告框出现后,用户需要点击确定按钮才能继续进行操作.语法:alert("文本 ...
- 第二阶段Sprint冲刺会议7
进展:试着把视频录制功能加到时间提醒中,但是整合没有成功,今天没有进展.
- max值——单元测试
设计思想 在调试的时候,尽可能的将所有可能出现的情况都考虑到,输入这些情况,查看程序运行的结果 源代码 #include<iostream> using namespace std; in ...
- TCP/IP,HTTP,HTTPS,WEBSocket协议
我看看着挺多的,我暂时没时间自己写,有需要的请借鉴 http://mp.weixin.qq.com/s?__biz=MzI0MDQ4MTM5NQ==&mid=2247486927&id ...
- python learning OOP2.py
class Student(object): pass s = Student() s.name = 'Chang' # 给一个实例动态绑定一个属性 print(s.name) def set_age ...
- 学习jenv
背景 生活不只是眼前的苟且, 还有诗和远方. 上个月工作需要启动了一个小项目, 按最初的计划会用JDK8. 但当计划报上去后, 运维部门出于后续升级维护的考虑, 不允许使用已经出来4年多的JDK8了, ...
- 关于supervisor无法监控golang代码的解决方法
之前一直都是使用如下方式运行go代码 # go run test.go 这种运行方式是直接编译运行go代码,虽然在调试的时候没出什么问题,但是在使用supervisor监控的时候,会提示如下错误:(b ...