#!/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:\Users\Administrator\Desktop\Python3_study\venv\Scripts\python.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 Files\Python36\lib\multiprocessing\process.py", line 105, in start self._popen = self._Popen(self) File "C:\Program Files\Python36\lib\multiprocessing\context.py", line 223, in _Popen return _default_context.get_context().Process._Popen(process_obj) File "C:\Program Files\Python36\lib\multiprocessing\context.py", line 322, in _Popen return Popen(process_obj) File "C:\Program Files\Python36\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__ reduction.dump(process_obj, to_child) File "C:\Program Files\Python36\lib\multiprocessing\reduction.py", line 60, in dump ForkingPickler(file, protocol).dump(obj) TypeError: can‘t 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:\Users\Administrator\Desktop\Python3_study\venv\Scripts\python.exe C:/Users/Administrator/Desktop/Python3_study/day10/进程间通信?.py [234, None, ‘Hello‘] Process finished with exit code 0
运行效果
原文地址:https://www.cnblogs.com/zhangmingda/p/9374998.html
时间: 2024-10-10 15:01:17