【Python】多进程-4
#练习:用event事件控制进程执行顺序,下面例子中,主进程main函数在创建了子进程之后,依然会往下执行,所以会出现主进程先打印出来的情况
import multiprocessing
import time def wait_for_event(e):
#Wait for the event to be set before doing anything
print 'wait_for_event: starting'
e.wait() # 等待收到能执行信号,如果一直未收到将一直阻塞
print 'wait_for_event: e.is_set()->', e.is_set() def wait_for_event_timeout(e, t):
#Wait t seconds and then timeout
print 'wait_for_event_timeout: starting'
e.wait(t)# 等待t秒超时,此时Event的状态仍未未设置,继续执行
print 'wait_for_event_timeout: e.is_set()->', e.is_set()
e.set()# 初始内部标志为真 if __name__ == '__main__':
e = multiprocessing.Event()
print "begin,e.is_set()", e.is_set()
w1 = multiprocessing.Process(name='block', target=wait_for_event, args=(e,))
w1.start() #可将2改为5,看看执行结果
w2 = multiprocessing.Process(name='nonblock', target=wait_for_event_timeout, args=(e, 2))
w2.start() #e.set() #可注释此句话看效果 print 'main: waiting before calling Event.set()'
time.sleep(3)
# e.set() #可注释此句话看效果
print 'main: event is set' #练习:管道练习,双工,单工,将受到的消息保存到文件中
import multiprocessing as mp
from multiprocessing import Process,Lock def write_file(content,lock,file_path="e:\\test40.txt"):
lock.acquire()
with open(file_path,"a") as f1:
f1.write(content+"\n")
lock.release() def proc_1(pipe,lock):
pipe.send('hello')
write_file("hello",lock)
print 'proc_1 received: %s' %pipe.recv()
pipe.send("what is your name?")
write_file("what is your name?",lock)
print 'proc_1 received: %s' %pipe.recv() def proc_2(pipe,lock):
print 'proc_2 received: %s' %pipe.recv()
pipe.send('hello, too')
write_file('hello, too',lock)
print 'proc_2 received: %s' %pipe.recv()
pipe.send("I don't tell you!")
write_file("I don't tell you!",lock) if __name__ == '__main__':
# 创建一个管道对象pipe
lock=Lock()
pipe = mp.Pipe()
print len(pipe)
print type(pipe)
# 将第一个pipe对象传给进程1
p1 = mp.Process(target = proc_1, args = (pipe[0],lock))
# 将第二个pipe对象传给进程2
p2 = mp.Process(target = proc_2, args = (pipe[1],lock))
p2.start() #这里按理说应该是收的先启起来,但这个例子里p1和p2哪个先启起来没关系
p1.start()
p2.join()
p1.join() #练习:condition,notify_all通知所有,这个例子里,有可能出现消费者收到消息较快,比生产者消息先打印出来的情况,如果使用notify(),就需要有几个进程就写几个notify()
import multiprocessing as mp
import threading
import time
def consumer(cond):
with cond:
print("consumer before wait")
cond.wait() # 等待消费
print("consumer after wait") def producer(cond):
with cond:
print("producer before notifyAll")
cond.notify_all() # 通知消费者可以消费了
print("producer after notifyAll") if __name__ == '__main__':
condition = mp.Condition() p1 = mp.Process(name = "p1", target = consumer, args=(condition,))
p2 = mp.Process(name = "p2", target = consumer, args=(condition,))
p3 = mp.Process(name = "p3", target = producer, args=(condition,)) p1.start()
time.sleep(2)
p2.start()
time.sleep(2)
p3.start()
【Python】多进程-4的更多相关文章
- Python多进程编程
转自:Python多进程编程 阅读目录 1. Process 2. Lock 3. Semaphore 4. Event 5. Queue 6. Pipe 7. Pool 序. multiproces ...
- Python多进程(1)——subprocess与Popen()
Python多进程方面涉及的模块主要包括: subprocess:可以在当前程序中执行其他程序或命令: mmap:提供一种基于内存的进程间通信机制: multiprocessing:提供支持多处理器技 ...
- Python多进程使用
[Python之旅]第六篇(六):Python多进程使用 香飘叶子 2016-05-10 10:57:50 浏览190 评论0 python 多进程 多进程通信 摘要: 关于进程与线程的对比, ...
- python多进程断点续传分片下载器
python多进程断点续传分片下载器 标签:python 下载器 多进程 因为爬虫要用到下载器,但是直接用urllib下载很慢,所以找了很久终于找到一个让我欣喜的下载器.他能够断点续传分片下载,极大提 ...
- Python多进程multiprocessing使用示例
mutilprocess简介 像线程一样管理进程,这个是mutilprocess的核心,他与threading很是相像,对多核CPU的利用率会比threading好的多. import multipr ...
- Python多进程并发(multiprocessing)用法实例详解
http://www.jb51.net/article/67116.htm 本文实例讲述了Python多进程并发(multiprocessing)用法.分享给大家供大家参考.具体分析如下: 由于Pyt ...
- python 多进程开发与多线程开发
转自: http://tchuairen.blog.51cto.com/3848118/1720965 博文作者参考的博文: 博文1 博文2 我们先来了解什么是进程? 程序并不能单独运行,只有将程 ...
- Python多进程----从入门到放弃
Python多进程 (所有只写如何起多进程跑数据,多进程数据汇总处理不提的都是耍流氓,恩,就这么任性) (1)进程间数据问题,因为多进程是完全copy出的子进程,具有独立的单元,数据存储就是问题了 ( ...
- day-4 python多进程编程知识点汇总
1. python多进程简介 由于Python设计的限制(我说的是咱们常用的CPython).最多只能用满1个CPU核心.Python提供了非常好用的多进程包multiprocessing,他提供了一 ...
- python 多进程 logging:ConcurrentLogHandler
python 多进程 logging:ConcurrentLogHandler python的logging模块RotatingFileHandler仅仅是线程安全的,如果多进程多线程使用,推荐 Co ...
随机推荐
- hadoopMR自定义输入类型
hadoop中的输入输出数据类型: BooleanWritable:标准布尔型数值 ByteWritable:单字节数值 DoubleWritable:双字节数值 FloatWritable:浮点数 ...
- dingyou-dingtalk-mobile在安卓系统上无法显示问题
dingyou-dingtalk-mobile项目在NowaUI上下载下来直接编译部署后在安卓版钉钉上会出现无法正常显示的问题,安卓真机调试在谷歌上显示错误如下:1.undefined is not ...
- UI基础一:简单的BOL查询
利用标准的BOL编辑工具,添加BOL对象,重写查询方法,实现简答的BOL查询 1.SE11创建查询对象结构: 2.SE11创建查询结果对象: 3.SE24新建处理类: 重写查询结果方法: METHOD ...
- css层叠性冲突中的优先级
一.首先从CSS级别来进行优先级划分: CSS控制页面样式的四种方法: 1.行内样式 通过style特性 <p style=”color:#F00; background:#CCC; font- ...
- 【Linux】shell学习之sed
sed替换命令 使用该命令,可以将特定字符串或匹配的规则表达式用另一个字符串替换. sed 's/88/--/' filename 将filename每行第一次出现的88用字符串--替换,然后将该文件 ...
- 在js中if条件为null/undefined/0/NaN/""表达式时,统统被解释为false,此外均为true
Boolean 表达式 一个值为 true 或者 false 的表达式.如果需要,非 Boolean 表达式也可以被转换为 Boolean 值,但是要遵循下列规则: 所有的对象都被当作 true. 当 ...
- spring boot整合shiro后,部分注解(Cache缓存、Transaction事务等)失效的问题
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/elonpage/article/details/78965176 前言 整合有缓存.事务的sprin ...
- Win10系列:JavaScript 项目模板中的文件和项模板文件
通过上面内容的学习,相信读者已经对各种项目模板和项模板有了大致的了解,本节将进一步介绍项目模板中默认包含的项目文件以及项模板文件,首先讲解这些文件中的初始内容以及作用,然后介绍在一个页面中如何添加控件 ...
- HotSpot虚拟机对象探秘
参考:http://www.infoq.com/cn/articles/jvm-hotspot
- 【原创】QT简单计算器
代码 //main.cpp #include "calculator_111.h" #include <QtWidgets/QApplication> int main ...