在python多进程中使用manager和Barrier
注意: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()

在python多进程中使用manager和Barrier的更多相关文章
- python多进程中的队列数据共享问题
0x00 起 今天在写一个小东西的时候,需要控制并发量,但又不能直接调用python multiprocessing(问题会在文后提到).于是尝试用Queue来实现. 最一开始的思路是这样的: fro ...
- python多进程-----multiprocessing包
multiprocessing并非是python的一个模块,而是python中多进程管理的一个包,在学习的时候可以与threading这个模块作类比,正如我们在上一篇转载的文章中所提,python的多 ...
- 一篇文章搞定Python多进程(全)
1.Python多进程模块 Python中的多进程是通过multiprocessing包来实现的,和多线程的threading.Thread差不多,它可以利用multiprocessing.Proce ...
- Python 多进程编程之 进程间的通信(在Pool中Queue)
Python 多进程编程之 进程间的通信(在Pool中Queue) 1,在进程池中进程间的通信,原理与普通进程之间一样,只是引用的方法不同,python对进程池通信有专用的方法 在Manager()中 ...
- python编程中的if __name__ == 'main与windows中使用多进程
if __name__ == 'main 一个python的文件有两种使用的方法,第一是直接作为程序执行,第二是import到其他的python程序中被调用(模块重用)执行. 因此if __name_ ...
- Python多进程库multiprocessing中进程池Pool类的使用[转]
from:http://blog.csdn.net/jinping_shi/article/details/52433867 Python多进程库multiprocessing中进程池Pool类的使用 ...
- Python程序中的进程操作-开启多进程(multiprocess.process)
目录 一.multiprocess模块 二.multiprocess.process模块 三.process模块介绍 3.1 方法介绍 3.2 属性介绍 3.3 在windows中使用process模 ...
- Python程序中的进程操作--—--开启多进程
Python程序中的进程操作-----开启多进程 之前我们已经了解了很多进程相关的理论知识,了解进程是什么应该不再困难了,刚刚我们已经了解了,运行中的程序就是一个进程.所有的进程都是通过它的父进程来创 ...
- python学习笔记——多进程中的锁Lock
1 进程锁 python编程中,引入了对象互斥锁的概念,来保证共享数据操作的完整性. 每个对象都对应于一个可称为“互斥锁”的标记,这个标记用来保证在任一时刻,只能有一线程访问对象. 在python中我 ...
随机推荐
- SPOJ 375 Query on a tree
Description 给出一个树,每条边有边权,支持两种操作,询问 \(u,v\) 路径上边权最大值,修改第 \(i\) 条边的边权,\(n\leqslant 10^4,T\leqslant 10\ ...
- curl模拟自动登陆&采集网页数据
<!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...
- 一个完整的Http请求
一个完整的http请求,通常有以下7点: 1.建立tcp连接 2.web浏览器web服务器发送请求命令 3.web浏览器发送请求头信息 4.web服务器应答 5.web服务器发送应答信息 6.web服 ...
- PyQt4软件打包成exe文件
使用py2exe进行打包 例: from distutils.core import setupimport py2exeimport sys sys.argv.append('py2exe') py ...
- ubuntu add application to launcher
eg. add sublime text to launcher so as to be found by launcher, docky, etc. add a file sudo gedit /u ...
- linux下git安装
Download for Linux and Unix It is easiest to install Git on Linux using the preferred package manage ...
- NGUI研究院之在Unity中使用贝塞尔曲线(六)[转]
鼎鼎大名的贝塞尔曲线相信大家都耳熟能详.这两天因为工作的原因需要将贝塞尔曲线加在工程中,那么MOMO迅速的研究了一下成果就分享给大家了哦.贝塞尔曲线的原理是由两个点构成的任意角度的曲线,这两个点一个是 ...
- /etc/securetty文件
/etc/securetty 文件允许你规定“root”用户可以从哪个tty设备登录. 登录程序(通常是/bin/login)需要读取“/etc/securetty”文件. 它的格式:列出来的tty设 ...
- ExpressQuantumGrid4的cxGrid的一些使用方法和经验
使用cxGrid有一些时间了,在这里总结一下使用cxGrid的一些方法,希望给刚开始接触cxGrid的人一些帮助. 1.简单介绍:cxGrid右下方的cxGrid1Level1是表示Grid表的层,c ...
- java一维数组
1.通过数组名进行赋值,其实质是引用 比如数组array1和数组array2 若执行array2=array1,实际上将array1的引用传递给array2,array1和array2 最后都指向同一 ...