我想尝试把子进程里的数据传输到主线程中,代码:
import multiprocessing
from multiprocessing import Pipe
from multiprocessing import Manager
def multi_try(start_i, end_i, send_end):
a = []
for i in range(start_i, end_i):
a.append(i)
send_end.send(a)
if __name__ == '__main__':
n_cpu = 5
procs = []
# manager = Manager()
# return_dict = manager.dict()
pipe_list = []
for sub_i in range(0, n_cpu):
recv_end, send_end = Pipe(False)
start_i = int(sub_i * 10 / n_cpu + 0.5)
end_i = int((sub_i + 1) * 10 / n_cpu + 0.5)
proc = multiprocessing.Process(target=multi_try, args=(start_i, end_i, send_end))
procs.append(proc)
pipe_list.append(recv_end)
for proc in procs:
proc.start()
for proc in procs:
proc.join()
result_list = [x.recv() for x in pipe_list]
运行结果:
[[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]
但是调试时显示:
Connected to pydev debugger (build 202.7660.27)
Traceback (most recent call last):
File "D:\install\PyCharm\PyCharm Community Edition 2020.2.3\plugins\python-ce\helpers\pydev_pydevd_bundle\pydevd_comm.py", line 301, in _on_run
r = r.decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 1023: unexpected end of data
请问各位该如何解决,万分感谢!
你这个进程之间的通信,直接用队列queue实现就好了