【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 ...
随机推荐
- Mybatis排序无效问题解决
在 mybatis 的 xml中,为一个SQL语句配置order by 子句时,需要这个排序的字段是前端传递过来的,而且排序的顺序(升序 OR 降序)也是由前端传递过来的.对于这种需求,我起初写成了下 ...
- charles抓包工具使用方法
注意: 1.软件安装证书,移动端安装证书: 2.设置SSL proxying setting: 3.设置Map Remote: 这里主要是为了设置移动端代理服务器,让其重定向到想要连接的环境上,比如手 ...
- zabbix_server.conf、zabbix_agentd.conf配置文件详解
zabbix_server.conf配置文件详解 AlertScriptsPath 默认值:/usr/local/share/zabbix/alertscripts 说明:告警脚本目录 AllowRo ...
- Linux第六周作业
一 实验过程 1 先进入LinuxKernel环境下,更新menu代码到最新版,用到的命令为rm menu -rf //强制删除当前menu,git clone http://git.shiyanlo ...
- react router @4 和 vue路由 详解(二)react-router @4用法
完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 2.react-router @4用法 a.大概目录 不需要像vue那样麻烦的 ...
- 【Loadrunner_WebService接口】对项目中的GetProduct接口生成性能脚本
一.环境 https://xxx.xxx.svc?wsdl 用户名:username 密码:password 对其中的GetProduct接口进行测试 备注:GetProducts.xml文件内容和S ...
- sql2008r2安装失败的解决办法
setup fails with: '.', hexadecimal value 0x00, is an invalid character.SQL 2012 Setup issues - hexad ...
- object遍历删除空值
export function deleteObjEmpty(search = {}) { for (let i in search) { search[i] == undef ...
- day26-python操作redis二
字符串的操作 #redis中的string 在内存中都是按照一个key对应一个valus来存储的 import redis pool = redis.ConnectionPool(host=" ...
- linux 基础储备
ls命令是Linux下最常用的命令之一,ls跟dos下的dir命令是一样的都是用来列出目录下的文件,下面我们就来一起看看ls的用法ls /home 这个命令不但可以添加用户到系统,而且可以默认为新用户 ...