2013-09-13 13:48 11613人阅读 评论(2) 收藏 举报
 分类:
Python(38) 

同步的方法基本与多线程相同。

1) Lock

当多个进程需要访问共享资源的时候,Lock可以用来避免访问的冲突。

  1. import multiprocessing
  2. import sys
  3. def worker_with(lock, f):
  4. with lock:
  5. fs = open(f,"a+")
  6. fs.write('Lock acquired via with\n')
  7. fs.close()
  8. def worker_no_with(lock, f):
  9. lock.acquire()
  10. try:
  11. fs = open(f,"a+")
  12. fs.write('Lock acquired directly\n')
  13. fs.close()
  14. finally:
  15. lock.release()
  16. if __name__ == "__main__":
  17. f = "file.txt"
  18. lock = multiprocessing.Lock()
  19. w = multiprocessing.Process(target=worker_with, args=(lock, f))
  20. nw = multiprocessing.Process(target=worker_no_with, args=(lock, f))
  21. w.start()
  22. nw.start()
  23. w.join()
  24. nw.join()
 
在上面的例子中,如果两个进程没有使用lock来同步,则他们对同一个文件的写操作可能会出现混乱。

2)Semaphore

Semaphore用来控制对共享资源的访问数量,例如池的最大连接数。

  1. import multiprocessing
  2. import time
  3. def worker(s,i):
  4. s.acquire()
  5. print(multiprocessing.current_process().name + " acquire")
  6. time.sleep(i)
  7. print(multiprocessing.current_process().name + " release")
  8. s.release()
  9. if __name__ == "__main__":
  10. s = multiprocessing.Semaphore(2)
  11. for i in range(5):
  12. p = multiprocessing.Process(target=worker, args=(s,i*2))
  13. p.start()

上面的实例中使用semaphore限制了最多有2个进程同时执行。

3)Event

Event用来实现进程间同步通信。

  1. import multiprocessing
  2. import time
  3. def wait_for_event(e):
  4. """Wait for the event to be set before doing anything"""
  5. print ('wait_for_event: starting')
  6. e.wait()
  7. print ('wait_for_event: e.is_set()->' + str(e.is_set()))
  8. def wait_for_event_timeout(e, t):
  9. """Wait t seconds and then timeout"""
  10. print ('wait_for_event_timeout: starting')
  11. e.wait(t)
  12. print ('wait_for_event_timeout: e.is_set()->' + str(e.is_set()))
  13. if __name__ == '__main__':
  14. e = multiprocessing.Event()
  15. w1 = multiprocessing.Process(name='block',
  16. target=wait_for_event,
  17. args=(e,))
  18. w1.start()
  19. w2 = multiprocessing.Process(name='non-block',
  20. target=wait_for_event_timeout,
  21. args=(e, 2))
  22. w2.start()
  23. time.sleep(3)
  24. e.set()
  25. print ('main: event is set')

#the output is:
#wait_for_event_timeout: starting
#wait_for_event: starting
#wait_for_event_timeout: e.is_set()->False
#main: event is set
#wait_for_event: e.is_set()->True

 
 

【python】多进程锁multiprocess.Lock的更多相关文章

  1. 多进程操作-进程锁multiprocess.Lock的使用

    多进程操作-进程锁multiprocess.Lock的使用 ​ 通过之前的Process模块的学习,我们实现了并发编程,虽然更加充分地利用了IO资源,但是也有缺陷:当多个进程共用一份数据资源的时候,就 ...

  2. python 多进程锁Lock和共享内存

    多进程锁 lock = multiprocessing.Lock() 创建一个锁 lock.acquire() 获取锁 lock.release() 释放锁 with lock: 自动获取.释放锁 类 ...

  3. 进程同步(multiprocess.Lock、multiprocess.Semaphore、multiprocess.Event) day38

    进程同步(multiprocess.Lock.multiprocess.Semaphore.multiprocess.Event) 锁 —— multiprocess.Lock 通过刚刚的学习,我们千 ...

  4. 铁乐学python_Day39_多进程和multiprocess模块2

    铁乐学python_Day39_多进程和multiprocess模块2 锁 -- multiprocess.Lock (进程同步) 之前我们千方百计实现了程序的异步,让多个任务可以同时在几个进程中并发 ...

  5. Python多进程编程-进程间协作(Queue、Lock、Semaphore、Event、Pipe)

    进程与进程之间是相互独立的,互不干扰.如果多进程之间需要对同一资源操作,就需要进程间共享变量,上一篇文章介绍了进程间共享数据的三大类Value.Array.Manager,这三种类的主要区别在于管理的 ...

  6. Python 之并发编程之进程中(守护进程(daemon)、锁(Lock)、Semaphore(信号量))

    五:守护进程 正常情况下,主进程默认等待子进程调用结束之后再结束守护进程在主进程所有代码执行完毕之后,自动终止kill -9 进程号 杀死进程.守护进程的语法:进程对象.daemon = True设置 ...

  7. python多进程与多线程编程

    进程(process)和线程(thread)是非常抽象的概念.多线程与多进程编程对于代码的并发执行,提升代码运行效率和缩短运行时间至关重要.下面介绍一下python的multiprocess和thre ...

  8. Python多进程与多线程编程及GIL详解

    介绍如何使用python的multiprocess和threading模块进行多线程和多进程编程. Python的多进程编程与multiprocess模块 python的多进程编程主要依靠multip ...

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

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

随机推荐

  1. Javascript闭包(狗血剧情,通俗易懂)

    我们先来看一个闭包的函数: function a() { var i = 0; function b() { alert(++i); } return b; } var c = a(); c(); c ...

  2. The Pragmatic Programmer Quick Reference Guide

    1.关心你的技艺 Care About Your Craft 如果不在乎能否漂亮地开发出软件,你又为何要耗费生命去开发软件呢? 2.思考!你的工作 Think! About Your Work 关掉自 ...

  3. 每日总结 -----把人家代码干掉了 我恨git

    今天搞了下午git,写完代码commit之后,pull完发现没法push,说是和origin有分支,然后自己查资料又是reset又是rebase的,commit之后发现自己改动的代码几乎没有被提交上去 ...

  4. 用ul 来制作导航栏的几个要点

    1 ul 1)list-style:none; 3)设置宽度 2)清除浮动的影响,使高度自适应 2 li 1)向左浮动 2)设置margin和padding都为0 3 a 1) dsiplay :bl ...

  5. 内存对齐 和 sizeof小结

    数据对齐(内存对齐)指该数据所在的地址必须是该数据长度的整数倍.X86CPU能直接访问对齐的数据,当它试图访问未对齐的数据时,会在内部进行一系列的调整,降低运行速度.数据对齐一般出现在结构体和类中,在 ...

  6. Liunx的各种小指令

    tsshutdown -y ----关闭所有服务tsboot -g COMMON ----启动公共服务tsboot -g ETXX   ----启动XX服务 tsma -e oet1 -l 172.X ...

  7. SRS文档

    1什么是用例? 在介始用例方法之前,我们首先来看一下传统的需求表述方式-"软件需求规约"(Software Requirement Specification).传统的软件需求规约 ...

  8. MMU内存管理单元相关知识点总结

    1.MMU是Memory Management Unit的缩写,中文名是内存管理单元,它是中央处理器(CPU)中用来管理虚拟存储器.物理存储器的控制线路,同时也负责虚拟地址映射为物理地址,以及提供硬件 ...

  9. C++ Daily 《5》----虚函数表的共享问题

    问题: 包含一个以上虚函数的 class B, 它所定义的 对象是否共用一个虚函数表? 分析: 由于含有虚函数,因此对象内存包含了一个指向虚函数表的指针,但是这个指针指向的是同一个虚函数表吗? 实验如 ...

  10. linux git 推送空文件夹

    /********************************************************************************* * linux git 推送空文件 ...