多进程进程间通讯multiprocessing

Posted zhangmingda

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多进程进程间通讯multiprocessing相关的知识,希望对你有一定的参考价值。

技术分享图片
#!/usr/bin/env python
# Author:Zhangmingda
import queue,threading
from multiprocessing import Process,Queue
def f(q):
    q.put([234,None,Hello])
if __name__ == __main__:
    q = queue.Queue() #使用线程队列运行报错TypeError: can‘t pickle _thread.lock objects
    p = Process(target=f,args=(q,)) #启动一个子进程
    # p = threading.Thread(target=f,) #启动线程直接共享主进程内存
    p.start()
    print(q.get())
    p.join()
线程报错
技术分享图片
C:UsersAdministratorDesktopPython3_studyvenvScriptspython.exe C:/Users/Administrator/Desktop/Python3_study/day10/进程间通信?.py
Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/Python3_study/day10/进程间通信?.py", line 13, in <module>
    p.start()
  File "C:Program FilesPython36libmultiprocessingprocess.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:Program FilesPython36libmultiprocessingcontext.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:Program FilesPython36libmultiprocessingcontext.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:Program FilesPython36libmultiprocessingpopen_spawn_win32.py", line 65, in __init__
    reduction.dump(process_obj, to_child)
  File "C:Program FilesPython36libmultiprocessing
eduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: cant pickle _thread.lock objects
运行报错效果
技术分享图片
#!/usr/bin/env python
# Author:Zhangmingda
import queue,threading
from multiprocessing import Process,Queue
def f(q):
    q.put([234,None,Hello])
if __name__ == __main__:
    # q = queue.Queue() #使用线程队列运行报错TypeError: can‘t pickle _thread.lock objects
    q = Queue() #进程队列
    p = Process(target=f,args=(q,)) #启动一个子进程
    # p = threading.Thread(target=f,) #启动线程直接共享主进程内存
    p.start()
    print(q.get())
    p.join()
进程间通讯

进程间通讯原理:本例中等于将父进程中的q克隆了一份给子进程,子进程进行了序列化,在子进程中执行后又反序列化给父进程。所以在父进程中能够得到子进程对父进程中数据的修改结果。

技术分享图片
C:UsersAdministratorDesktopPython3_studyvenvScriptspython.exe C:/Users/Administrator/Desktop/Python3_study/day10/进程间通信?.py
[234, None, Hello]

Process finished with exit code 0
运行效果

 

以上是关于多进程进程间通讯multiprocessing的主要内容,如果未能解决你的问题,请参考以下文章

Python 分布式进程间通讯

python 学习笔记 - Queue & Pipes,进程间通讯

进程间通讯与进程池 -- 2019-08-11 18:46:25

python 进程

进程间通讯的三种方式

python基础 多进程 进程间通信 multiprocess