Python实现多进程共享数据的方法分析

yipeiwu_com5年前Python基础

本文实例讲述了Python实现多进程共享数据的方法。分享给大家供大家参考,具体如下:

示例一:

# -*- coding:utf-8 -*-
from multiprocessing import Process, Manager
import time
import random
def kkk(a_list, number):
  for i in range(10):
    a_list.append(i)
    time.sleep(random.randrange(2))
    print('这是进程{} {}'.format(number, a_list))
  print('这是进程{} {}'.format(number, a_list))
def jjj(a_list):
  for i in range(2):
    process = Process(target=kkk, args=(a_list, i))
    process.start()
if __name__ == '__main__':
  a_list = []
  process_0 = Process(target=jjj, args=(a_list,))
  process_0.start()
  process_0.join()
  print(a_list)
  print(len(a_list))
  print('it\'s ok')

输出:

这是进程0 [0]
这是进程0 [0, 1]
这是进程0 [0, 1, 2]
这是进程0 [0, 1, 2, 3]
这是进程1 [0]
这是进程0 [0, 1, 2, 3, 4]
这是进程1 [0, 1]
这是进程0 [0, 1, 2, 3, 4, 5]
这是进程1 [0, 1, 2]
这是进程1 [0, 1, 2, 3]
这是进程1 [0, 1, 2, 3, 4]
这是进程1 [0, 1, 2, 3, 4, 5]
这是进程0 [0, 1, 2, 3, 4, 5, 6]
这是进程0 [0, 1, 2, 3, 4, 5, 6, 7]
这是进程0 [0, 1, 2, 3, 4, 5, 6, 7, 8]
这是进程1 [0, 1, 2, 3, 4, 5, 6]
这是进程0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
这是进程0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
这是进程1 [0, 1, 2, 3, 4, 5, 6, 7]
这是进程1 [0, 1, 2, 3, 4, 5, 6, 7, 8]
这是进程1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
这是进程1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[]
0
it's ok
Process finished with exit code 0

示例二:

使用 Manager

# -*- coding:utf-8 -*-
from multiprocessing import Process, Manager
import time
import random
def kkk(a_list, number):
  for i in range(10):
    a_list.append(i)
    time.sleep(random.randrange(2))
    print('这是进程{} {}'.format(number, a_list))
  print('这是进程{} {}'.format(number, a_list))
def jjj(a_list):
  for i in range(2):
    process = Process(target=kkk, args=(a_list, i))
    process.start()
if __name__ == '__main__':
  manager = Manager()
  a_list = manager.list()
  # a_list = []
  process_0 = Process(target=jjj, args=(a_list,))
  process_0.start()
  process_0.join()
  print(a_list)
  print(len(a_list))
  print('it\'s ok')

输出:

这是进程0 [0, 0]
这是进程0 [0, 0, 1]
这是进程0 [0, 0, 1, 2]
这是进程0 [0, 0, 1, 2, 3]
这是进程0 [0, 0, 1, 2, 3, 4]
这是进程1 [0, 0, 1, 2, 3, 4, 5]
这是进程0 [0, 0, 1, 2, 3, 4, 5, 1]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5]
这是进程0 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6]
这是进程0 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7]
这是进程0 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7]
这是进程0 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8]
这是进程0 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8, 9]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8, 9]
[0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8, 9]
20
it's ok
Process finished with exit code 0

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python进程与线程操作技巧总结》、《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

用python处理MS Word的实例讲解

用python处理MS Word的实例讲解

使用python工具读写MS Word文件(docx与doc文件),主要利用了python-docx包。本文给出一些常用的操作,并完成一个样例,帮助大家快速入手。 安装 pyhton处理...

django将网络中的图片,保存成model中的ImageField的实例

有这样的情形,django个人头像在model中是: class UserProfile(AbstractUser): """ 用户 """ name = models.Ch...

Python TCP通信客户端服务端代码实例

这篇文章主要介绍了Python TCP通信客户端服务端代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 TCP客户端: im...

Python切片操作实例分析

本文实例讲述了Python切片操作。分享给大家供大家参考,具体如下: 在很多编程语言中,针对字符串提供了截取函数,其实目的就是对字符串切片。Python没有针对字符串的截取函数,只需要切...

对Tensorflow中的变量初始化函数详解

Tensorflow 提供了7种不同的初始化函数: tf.constant_initializer(value) #将变量初始化为给定的常量,初始化一切所提供的值。 假设在卷积层中,...