在python多进程中使用manager和Barrier
2016-12-01 11:04
337 查看
注意:Barrier是PYTHON3才有的功能,在2中无法测试。
#!/usr/bin/env python # -*- coding: utf-8 -*- import multiprocessing from multiprocessing import Barrier, Lock, Process from time import time from datetime import datetime def test_with_barrier(synchronizer, seializer): name = multiprocessing.current_process().name synchronizer.wait() now = time() with serializer: print("process %s -----> %s" % (name, datetime.fromtimestamp(now))) def test_without_barrier(): name = multiprocessing.current_process().name now = time() print("process %s -----> %s" % (name, datetime.fromtimestamp(now))) def worker(dictionary, key, item): dictionary[key] = item print(key, item) if __name__ == '__main__': synchronizer = Barrier(2) serializer = Lock() Process(name='p1 - test_with_barrier', target=test_with_barrier, args=(synchronizer, serializer)).start() Process(name='p2 - test_with_barrier', target=test_with_barrier, args=(synchronizer, serializer)).start() Process(name='p3 - test_without_barrier', target=test_without_barrier).start() Process(name='p4 - test_without_barrier', target=test_without_barrier).start() mgr = multiprocessing.Manager() dictionary = mgr.dict() jobs = [multiprocessing.Process\ (target=worker, args=(dictionary, i, i*2)) for i in range(10) ] for j in jobs: j.start() for j in jobs: j.join()
![](https://images2015.cnblogs.com/blog/465438/201612/465438-20161201110345568-1705635156.png)
相关文章推荐
- python多进程中使用pool
- 浅析Python中的多进程与多线程的使用
- python多进程multiprocessing的简单使用
- python(34):为什么在Python里推荐使用多进程而不是多线程?
- 为什么在Python里推荐使用多进程而不是多线程?
- Python使用多进程(核)外壳
- 为什么在Python里推荐使用多进程而不是多线程?
- python使用多进程或者多线程
- Python多进程multiprocessing使用示例
- 为什么在Python里推荐使用多进程而不是多线程?
- [Python]django使用多进程连接msyql错误
- 转载:浅析Python中的多进程与多线程的使用
- Python多进程不要使用TimedRotatingFileHandler
- python多进程与多线程使用场景
- Python中的多进程与多线程/分布式该如何使用
- 使用jython和ScriptEngineManager在java中执行python
- 详解Python的Django框架中Manager方法的使用
- python中多进程+协程的使用以及为什么要用它
- Python中使用多进程复制文件
- 为什么在Python里推荐使用多进程而不是多线程?