python 多进程共享数据的读与写
来源:互联网 发布:普联软件 怎么样 编辑:程序博客网 时间:2024/05/16 17:36
1. 父进程向子进程传参
1.1python通常的数据结构可以传给子进程读,但子进程写无效:
from multiprocessing import Pool, Managerdef 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, Managerdef 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}
阅读全文
0 0
- python 多进程共享数据的读与写
- Python中多进程之间的数据共享
- Python的进程实现数据共享
- Python 多进程 —— 共享数据
- 多进程写文件的共享问题:
- python 进程间共享数据
- python进程间共享数据
- python多进程入门、分布式进程数据共享
- python进程写文件与多进程记录日志
- python基础-Manager进程数据共享、进程互斥锁数据非共享、线程互斥锁数据共享
- python 进程间共享数据 (一)
- python 进程间共享数据 (二)
- python 进程间共享数据 (三)
- Python 进程之间共享数据(全局变量)
- Python 进程之间共享数据(全局变量)
- 进程间的数据共享
- python 多进程共享变量
- python多进程共享内存
- leetcode.566.Reshape the Matrix
- 如何定义一个不能被继承的类
- centos7中keepalived配置
- FreeSWITCH 视频会议常用命令
- TensorFlow 队列与多线程
- python 多进程共享数据的读与写
- vijos1143
- Android Binder 作为 IPC 机制原理和面试回答
- HTML 基础2
- 智能家居是真蓝海还是伪需求?
- boost-asio学习1——定时器(同步、异步)
- ajax 调用 java webapi 多个参数(一)
- mysql创建外键不起作用
- 分布式消息队列kafka原理简介