【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 ...
随机推荐
- element-ui table中排序 取消表格默认排序问题
sortTable 设置为 custom 一定要设置在列上
- HDU-2874-森林求LCA/tarjan
http://acm.hdu.edu.cn/showproblem.php?pid=2874 给出一个森林,询问任意两点最短距离. tarjan跑一遍即可,就是这个题卡内存,vector会MLE,换前 ...
- bootstrap居中
1.页面 <div class="container"> <div class="row clearfix"> <div clas ...
- spring cloud服务发现注解之@EnableDiscoveryClient与@EnableEurekaClient区别
在使用服务发现的时候有两种注解, 一种为@EnableDiscoveryClient, 一种为@EnableEurekaClient, 用法上基本一致,下文是从stackoverflow上面找到的对这 ...
- 【LeetCode】Valid Parentheses合法括号
给定一个仅包含 '('.')'.'{'.'}'.'['.']'的字符串,确定输入的字符串是否合法. e.g. "()"."()[]{}"."[()]( ...
- git设置远程同步分支
git push --set-upstream origin yourBranchName
- 【转】JavaScript => TypeScript 入门
几个月前把 ES6 的特性都过了一遍,收获颇丰.现在继续来看看 TypesScript(下文简称为 “TS”).限于经验,本文一些总结如有不当,欢迎指正. 官网有这样一段描述: TypeScript ...
- VSS迁移详细教程
本文默认迁移机和目标机已是安装好VSS服务,如果没装好参见VSS+SourceAnywhere for VSS搭建版本控制系统教程 如果你只想以最快的速度迁移库而并不关心VSS的一些操作使用,那么可直 ...
- 转: Linux mount/unmount命令
https://blog.csdn.net/okhymok/article/details/76616892 楼主具体哪里转的 我不清楚 好像没看到原始出处 开机自动挂载 如果我们想实现开机自动挂载某 ...
- tomcat访问端口
问题描述:今天,访问服务器上的应用,tomcat服务器已经启动,确怎么都打不开:问题原因:原来有人改了端口.具体操作:进入tomcat的安装目录,进入conf文件夹下,找到server.xml文件.用 ...