python 多进程共享数据的读与写
2017-07-05 14:32
411 查看
1. 父进程向子进程传参
1.1python通常的数据结构可以传给子进程读,但子进程写无效:
from multiprocessing import Pool, Manager def chid_proc(test_dict, i): print '{} process, before modify: {}'.format(i, test_dict) test_dict[i] = i * i print '{} process, after modify: {}'.format(i, test_dict) if __name__ == '__main__': td = {'a':1} pool = Pool(3) for i in xrange(0, 3): pool.apply_async(chid_proc, args=(td, i)) pool.close() pool.join() print 'after pool: {}'.format(td)
结果:
0 process, before modify: {'a': 1} 0 process, after modify: {'a': 1, 0: 0} 1 process, before modify: {'a': 1} 2 process, before modify: {'a': 1} 1 process, after modify: {'a': 1, 1: 1} 2 process, after modify: {'a': 1, 2: 4} after pool: {'a': 1}
1.2 使用Manager.dict实现进程间共享数据的修改
from multiprocessing import Pool, Manager def chid_proc(test_dict, i): print '{} process, before modify: {}'.format(i, test_dict) test_dict[i] = i * i print '{} process, after modify: {}'.format(i, test_dict) if __name__ == '__main__': #td = {'a':1} td = Manager().dict() td['a'] = 1 pool = Pool(3) for i in xrange(0, 3): pool.apply_async(chid_proc, args=(td, i)) pool.close() pool.join() print 'after pool: {}'.format(td)
结果:
0 process, before modify: {'a': 1} 0 process, after modify: {'a': 1, 0: 0} 1 process, before modify: {'a': 1, 0: 0} 1 process, after modify: {'a': 1, 0: 0, 1: 1} 2 process, before modify: {'a': 1, 0: 0, 1: 1} 2 process, after modify: {'a': 1, 0: 0, 2: 4, 1: 1} after pool: {'a': 1, 0: 0, 2: 4, 1: 1}
相关文章推荐
- 11.python并发入门(part10 多进程之间实现通信,以及进程之间的数据共享)
- python并发编程之多进程2-------------数据共享及进程池和回调函数
- Python中多进程之间的数据共享
- Python 多进程 —— 共享数据
- Nodejs中cluster模块的多进程共享数据问题
- multiprocessing在python中的高级应用-共享数据与同步
- python 进程间共享数据
- python 进程间共享数据 (一)
- python unittest TestCase间共享数据(全局变量的使用)
- python多进程共享变量Value使用tips
- Python读取Postgresql后将数据传递给C++共享库
- Nodejs中cluster模块的多进程共享数据问题
- Learning Spark (Python版) 学习笔记(二)----键值对、数据读取与保存、共享特性
- python多进程共享变量
- 【原】Learning Spark (Python版) 学习笔记(二)----键值对、数据读取与保存、共享特性
- python多进程中的队列数据共享问题
- 获取已经安装APK的路径 多进程Preferences数据共享 泛型ArrayList转数组 保存恢复ListView当前位置 调用便携式热点和数据共享设置 格式化 文件夹排
- python从共享目录读取考勤数据,通过openpyxl解析excel2007,查询出自己考勤记录
- python 进程间共享数据 (三)
- python 多进程共享变量