【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 ...
随机推荐
- [洛谷 P3788] 幽幽子吃西瓜
妖梦费了好大的劲为幽幽子准备了一个大西瓜,甚至和兔子铃仙打了一架.现在妖梦闲来无事,就蹲在一旁看幽幽子吃西瓜.西瓜可以看作一个标准的球体,瓜皮是绿色的,瓜瓤是红色的,瓜皮的厚度可视为0.妖梦恰好以正视 ...
- eclipse初学者的使用
eclipse的使用 字体设置: 设置环境字符: 设置背景颜色 寻找窗口: java JDK的配置: 配置自动提示: 适合初学者适应环境开发.
- Utils--前台调用后台接口工具类
Utils--前台调用后台接口工具类 package com.taotao.manage.httpclient; import java.io.IOException; import java.net ...
- burpsuite拦截https数据包(Firefox)
1.配置浏览器对http/https都使用burpsuite代理 http和https是分开的,对http使用了代理并不代表对https也使用了代理,要配置浏览器让其对https也使用同样的代理. 当 ...
- 牛客网 PAT 算法历年真题 1002 :数字分类 (20)
1002 :数字分类 (20) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 给定一系列正整数,请按要求对数字 ...
- Qt动态布局
QVBoxLayout *m_pvLayout = NULL: QWidget *m_pWidgetPlay = NULL: m_pvLayout = new QVBoxLayout(this); m ...
- Java中static的用法,初始化块
使用 Arrays 类操作 Java 中的数组语法: Arrays.sort(数组名); 可以使用 sort( ) 方法实现对数组的排序,只要将数组名放在 sort( ) 方法的括号中,就可以完成对该 ...
- win10打文件预览功能
- 第三组 通信一班 030 IPv6 RIPng (PT)
实验拓扑 地址规划 设备 接口 IPV6 地址/掩码 PC0 / 2001:DB8:30:2:201:42FF:FE8A:7688/64 PC1 / 2001:DB8:30:1:230:A3FF:F ...
- Android : 输入设备键值从底层到应用层的映射流程
一.Android输入子系统简介: Android输入事件的源头是位于/dev/input/下的设备节点,而输入系统的终点是由WMS管理的某个窗口.最初的输入事件为内核生成的原始事件,而最终交付给窗口 ...