Python通过4种方式实现进程数据通信

作者:我太难了008 时间:2023-11-04 15:13:48 

python提供了4种方式来满足进程间的数据通信

1. 使用multiprocessing.Queue可以在进程间通信,但不能在Pool池创建的进程间进行通信

2. 使用multiprocessing.Manager.Queue可以在Pool进程池创建的进程间进行通信

3. 通过Pipe进行线程间的通信, pipe进程间通信的性能高于Queue,但是它只能在两个进程间进行通信

4. 使用Manager类提供的数据结构可以进行进程间的通信


from multiprocessing import Process, Queue, Pool, Manager, Pipe
# 注意线程间的通信,使用的queue.Queue
# from queue import Queue
import time

# 1. 使用multiprocessing.Queue可以在进程间通信

# def producer(queue):
#   queue.put('A')
#   time.sleep(2)
#
# def consumer(queue):
#   time.sleep(2)
#   data = queue.get()
#   print(data)
#
# if __name__ == '__main__':
#   queue= Queue(10)
#   p = Process(target=producer, args=(queue,))
#   c = Process(target=consumer, args=(queue,))
#   p.start()
#   c.start()
#   p.join()
#   c.join()

# 2. 使用共享全局变量,在多进程间通信(结论: 不行)
# def producer(a):
#   a += 1
#   time.sleep(2)
#
#
# def consumer(a):
#   time.sleep(2)
#   print(a)
#
# if __name__ == '__main__':
#   a = 1
#   p = Process(target=producer, args=(a,))
#   c = Process(target=consumer, args=(a,))
#   p.start()
#   c.start()
#   p.join()
#   c.join()

# 3. multiprocessing.Queue不能用于multiprocessing.Pool进程池创建的进程间进行通信
# def producer(queue):
#   queue.put('A')
#   time.sleep(2)
#
#
# def consumer(queue):
#   time.sleep(2)
#   data = queue.get()
#   print("consumer:%s" % data)
#
#
# if __name__ == '__main__':
#   # queue = Queue(10) # 这个是使用multiprocessing.Queue,无效
#   queue = Manager().Queue(10) # 这个是使用multiprocessing.Manager.Queue, 可以
#   pool = Pool(2)
#   pool.apply_async(producer, args=(queue,))
#   pool.apply_async(consumer, args=(queue,))
#   pool.close()
#   pool.join()

# 4.通过Pipe进行线程间的通信, pipe进程间通信的性能高于Queue
# def producer(pipe):
#   pipe.send('admin')
#
#
# def consumer(pipe):
#   data = pipe.recv()
#   print("consumer:%s" % data)
#
#
# if __name__ == '__main__':
#   receive_pipe, send_pipe = Pipe()
#   """Pipe只能适应于两个进程间的通信"""
#   p = Process(target=producer, args=(send_pipe,))
#   c = Process(target=consumer, args=(receive_pipe,))
#   p.start()
#   c.start()
#   p.join()
#   c.join()

# 5. 进程间通信的其它方式

def add_data(p_dict, key, value):
 p_dict[key] = value

if __name__ == '__main__':
 progress_dict = Manager().dict() #Manager()类中提供的数据结构都能够做到进程的通信
 first_progress = Process(target=add_data, args=(progress_dict, 'name', 'admin',))
 second_progress = Process(target=add_data, args=(progress_dict, 'age', 45,))
 first_progress.start()
 second_progress.start()
 first_progress.join()
 second_progress.join()
 print(progress_dict) #{'age': 45, 'name': 'admin'}

来源:https://www.cnblogs.com/z-qinfeng/p/12064529.html

标签:python,进程,数据,通信
0
投稿

猜你喜欢

  • 再谈 Web 字体的现状与未来[译]

    2009-11-24 13:55:00
  • OBJECTPROPERTY与sp_rename更改对象名称的介绍

    2012-01-29 18:04:39
  • 用户界面设计中“状态”和“动作”的表达

    2011-01-06 12:36:00
  • 何在MySQL数据库中定义外键

    2009-12-17 12:29:00
  • 在ASP.NET 2.0中操作数据之二十七:创建自定义排序用户界面

    2023-06-25 21:01:05
  • asp如何做一个树状展开视图来显示自己的记录结构?

    2010-07-12 18:56:00
  • SHA256算法 asp源码

    2009-08-28 13:01:00
  • php strftime函数获取日期时间(switch用法)

    2023-06-11 13:26:33
  • 什么是XML?

    2007-10-29 12:53:00
  • EXEC(EXECUTE)函数访问INSERTED或DELETED的内部临时触发表

    2012-01-29 18:07:30
  • JS打开新窗口的2种方式

    2023-07-07 02:44:49
  • 如何编写一个基于WEB的文件查询系统?

    2009-11-08 18:55:00
  • asp + oracle 分页方法

    2010-05-11 20:09:00
  • Windows安装Anaconda并且配置国内镜像的详细教程

    2023-07-06 13:45:15
  • ASP 日期的加减运算实现代码

    2011-03-08 10:47:00
  • 用户不需要信息快餐

    2009-02-25 12:34:00
  • 优化你的ASP程序及优化网页

    2007-10-06 23:02:00
  • PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程

    2023-11-19 20:31:59
  • 教你在SQL Server 2000数据库中使用分区

    2008-11-25 11:55:00
  • 网页中的平衡、对比、连贯和留白

    2008-11-24 12:11:00
  • asp之家 网络编程 m.aspxhome.com