#练习:用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的更多相关文章

  1. Python多进程编程

    转自:Python多进程编程 阅读目录 1. Process 2. Lock 3. Semaphore 4. Event 5. Queue 6. Pipe 7. Pool 序. multiproces ...

  2. Python多进程(1)——subprocess与Popen()

    Python多进程方面涉及的模块主要包括: subprocess:可以在当前程序中执行其他程序或命令: mmap:提供一种基于内存的进程间通信机制: multiprocessing:提供支持多处理器技 ...

  3. Python多进程使用

    [Python之旅]第六篇(六):Python多进程使用   香飘叶子 2016-05-10 10:57:50 浏览190 评论0 python 多进程 多进程通信 摘要:   关于进程与线程的对比, ...

  4. python多进程断点续传分片下载器

    python多进程断点续传分片下载器 标签:python 下载器 多进程 因为爬虫要用到下载器,但是直接用urllib下载很慢,所以找了很久终于找到一个让我欣喜的下载器.他能够断点续传分片下载,极大提 ...

  5. Python多进程multiprocessing使用示例

    mutilprocess简介 像线程一样管理进程,这个是mutilprocess的核心,他与threading很是相像,对多核CPU的利用率会比threading好的多. import multipr ...

  6. Python多进程并发(multiprocessing)用法实例详解

    http://www.jb51.net/article/67116.htm 本文实例讲述了Python多进程并发(multiprocessing)用法.分享给大家供大家参考.具体分析如下: 由于Pyt ...

  7. python 多进程开发与多线程开发

    转自: http://tchuairen.blog.51cto.com/3848118/1720965 博文作者参考的博文:  博文1  博文2 我们先来了解什么是进程? 程序并不能单独运行,只有将程 ...

  8. Python多进程----从入门到放弃

    Python多进程 (所有只写如何起多进程跑数据,多进程数据汇总处理不提的都是耍流氓,恩,就这么任性) (1)进程间数据问题,因为多进程是完全copy出的子进程,具有独立的单元,数据存储就是问题了 ( ...

  9. day-4 python多进程编程知识点汇总

    1. python多进程简介 由于Python设计的限制(我说的是咱们常用的CPython).最多只能用满1个CPU核心.Python提供了非常好用的多进程包multiprocessing,他提供了一 ...

  10. python 多进程 logging:ConcurrentLogHandler

    python 多进程 logging:ConcurrentLogHandler python的logging模块RotatingFileHandler仅仅是线程安全的,如果多进程多线程使用,推荐 Co ...

随机推荐

  1. WDA基础五:ALV组件的使用

    说明,因为ALV的功能比TABLE强大,所以在很多时候都习惯性的选择ALV显示. ALV是WDA的组件,封装好的,和SELECT OPTION一样.所以使用的方法一般就是:引入组件,初始化组件,数据绑 ...

  2. ASP.NET 后台页面无法识别服务器控件ID

    在学习asp.net 的时候 发现有个页面服务器控件无法识别,提示未知元素 解决方法 将不能识别服务器控件ID 的后台文件 类名改写,重新生成一次. 然后再改回来就可以了.

  3. Rail_UVa514_栈

    /* https://vjudge.net/problem/UVA-514 */ #include "pch.h" #include<iostream> #includ ...

  4. InnoDB存储引擎介绍-(3)InnoDB缓冲池配置详解

    原文链接  http://www.ywnds.com/?p=9886 一.InnoDB缓冲池 InnoDB维护一个称为缓冲池的内存存储区域 ,用于缓存内存中的数据和索引.了解InnoDB缓冲池的工作原 ...

  5. PostgreSQL数据库单机扩展为流复制

    primary:10.189.102.118 standby:10.189.100.195 1. 配置ssh互信机制 在primary主库执行 $ ssh-keygen -t rsa $ cp ~/. ...

  6. 【Python】Anaconda配置

    Anaconda 是一个用于科学计算的Python发行版,支持 Linux.Mac.Windows 系统,提供了包管理与环境管理的功能,可以很方便地解决多版本 Python 并存.切换以及各种第三方包 ...

  7. 一、J2EE

    一.HTTP协议中的响应代码 响应代码从1xx--5xx一共有41中.常见的 404:表示访问的页面不存在.这表示一个浏览器的错误,就是服务端没有提供这个服务,你却去访问.这个锅要算在浏览器头上,而不 ...

  8. Linux -- 基于zookeeper的java api(二)

    Linux -- 基于zookeeper的java api(二) 写一个关于基于集群的zookeeper的自定义实现HA 基于客户端和监控器:使用监控的方法查看每个注册过的节点的状态来做出操作. Wa ...

  9. [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆

    [Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...

  10. redis集群搭建教程(以3.2.2为例)

    redis从3.0版本开始支持集群,2.X版本主支持sentinel主从模式:所以要搭建集群务必下载3.0以上版本,本教程以3.2.2版本为例. redis集群最少要有3个主节点,最典型的是3主3从组 ...