进程间同步,可以使用lock进行控制。

官方文档的例子如下:

from multiprocessing import Process, Lock

def f(l, i):
l.acquire()
print 'hello world', i
l.release() if __name__ == '__main__':
lock = Lock() for num in range(10):
Process(target=f, args=(lock, num)).start()

运行结果:

hello world 0
hello world 1
hello world 2
hello world 3
hello world 4
hello world 5
hello world 6
hello world 7
hello world 8
hello world 9

or

from multiprocessing import Process, Lock
from time import sleep def f(l, i):
l.acquire()
print 'hello world', i
l.release() def n(l,i):
l.acquire()
print 'hello world', i
sleep(5)
l.release() if __name__ == '__main__':
lock = Lock()
result = [] result.append(Process(target = n,args = (lock,'first')))
result.append(Process(target = f,args = (lock,'sec'))) for x in result:
x.start() for x in result:
x.join() print('main process run OK')

运行结果:

hello world first
中间等待5秒钟
hello world sec
main process run OK

  

  

  

  

python Synchronization between processes的更多相关文章

  1. Working with Python subprocess - Shells, Processes, Streams, Pipes, Redirects

    Posted: 2009-04-28 15:20 Tags: Python Note Much of the "What Happens When you Execute a Command ...

  2. python 2016 大会 pyconsk ppt ---python dtrace

    https://github.com/pyconsk/2016-slides PyCon SK 2016 - March 2016 1DTrace and PythonJesús Cea Aviónj ...

  3. Python并行(parallel)之谈

    简介 可以先看看并发Concurrent与并行Parallel的区别 在谈并行前,头脑中总会浮出多线程.多进程.线程/进程同步.线程/进程通信等词语. 那为什么需要同步.通信,它们之间的作用是怎样的呢 ...

  4. windows和linux中搭建python集成开发环境IDE——如何设置多个python环境

    本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...

  5. Building Python 2.7.10 with Visual Studio 2010 or 2015 - Google Chrome

    您的浏览器(Chrome 33) 需要更新.该浏览器有诸多安全漏洞,无法显示本网站的所有功能. 了解如何更新浏览器 × p-nand-q.com C++  Python  Programming  L ...

  6. Synchronization (computer science)

    过程同步.数据同步. In computer science, synchronization refers to one of two distinct but related concepts: ...

  7. 像职业选手样编码:地道Python

    Code Like a Pythonista: Idiomatic Python David Goodger goodger@python.org http://python.net/~goodger ...

  8. (转) [it-ebooks]电子书列表

    [it-ebooks]电子书列表   [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...

  9. 笔记-python-standard library-17.2 multiprocessing

    笔记-python-standard library-17.2 multiprocessing 1.      multiprocessing source code:Lib/multiprocess ...

随机推荐

  1. java 多线程Callable和Runable执行顺序问题详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt125 毫无疑问 Runnable会进行异步执行,此处不多说,主要说明Call ...

  2. 【DDD】领域驱动设计实践 —— 限界上下文识别

    本文从战略层面街上DDD中关于限界上下文的相关知识,并以ECO系统为例子,介绍如何识别上下文.限界上下文(Bounded Context)定义了每个模型的应用范围,在每个Bounded Context ...

  3. tkinter第一章1

    tk1 ------------------------------------------------------------------------------------------ impor ...

  4. 大数的减法函数--c语言

    代码展示:   http://paste.ubuntu.com/23693598/ #include<stdio.h> #include<stdlib.h> #include& ...

  5. java aio nio bio

    转自:http://blog.csdn.NET/liuxiao723846/article/details/45066095 Java中的IO主要源自于网络和本地文件 IO的方式通常分为几种,同步阻塞 ...

  6. PCB Design_经验之谈

    所谓覆铜,就是将PCB上闲置的空间作为基准面,然后用固体铜填充,这些铜区又称为灌铜.敷铜的意义在于,减小地线阻抗,提高抗干扰能力:降低压降,提高电源效率:与地线相连,还可以减小环路面积.也出于让PCB ...

  7. 看懂c/c++ 函数、指针、数组定义

    读懂 函数 + 指针 + 数组 c语言运算符机器优先级,看这里 结合运算符优先级,我们试着读懂函数和指针 优先级简单看 表达式提升():一级优先 函数():二级优先 数组[]:二级优先 指针定义*:三 ...

  8. 201521123107 《Java程序设计》第2周学习总结

    第2周作业-Java基本语法与类库 1.本周学习总结 要点主要有: (1)String类 String类是本周的一个重点,String类的对象是不可变的,即String对象后就在内存中开辟了一个字符串 ...

  9. 【Alpha】 第七次Daily Scrum Meeting

    一.本次会议为第七次meeting会议 二.时间:9:37AM-9:50AM 地点:禹州三楼 三.会议站立式照片 四.今日任务 成员 昨日任务 今日任务 林清青 学习并了解微信程序相关方面知识,为小组 ...

  10. 201521123098 《Java程序设计》第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 1. 在编写类时,要有写构造函数和各变量的getter()和setter()方法的习惯: 2. 同步操作时要记得在 ...