在Python中的进程之间共享列表的正确方法?

时间:2018-10-31 06:58:48

标签: python python-3.x python-multiprocessing

问题

我想建立两个子流程,其中子流程1继续生成数据(以“列表”的类型),而子流程2负责处理从子流程1发送的数据。

我使用multiprocessing.Manager()。list()创建了一个共享列表。但这是它报告的错误:

  

FileNotFoundError:[WinError 2]

代码

我将代码简化如下: ps:需要在终端中运行。

import multiprocessing as mp
import random
import time

def generator(a,b,tick):#simulating data collection,and a list will be generated at random and passed to another shared list.
    counter=0
    while True:
        time.sleep(1)
        a.append([random.uniform(1,5),random.uniform(1,5),random.uniform(1,5),random.uniform(1,5)])
        counter+=1
        print('generate says',a[:])
        if counter%5==0:
            b.append(a[:])
            tick.value=1#Telling 'printer' func to print.
            for _ in a:
                a.remove(_)

def printer(b,tick):#simulating data processing, and only printing data received from the 'generator' func here.
    while True:
        time.sleep(1)
        if tick.value==1:
            time.sleep(1)
            print('printer says',b[:])
            tick.value=0 
            for _ in b:
                b.remove(_)

if __name__=='__main__':
    tick=mp.Value('i',0)
    a=mp.Manager().list()
    b=mp.Manager().list()
    p1=mp.Process(target=generator,args=(a,b,tick))
    p2=mp.Process(target=printer,args=(b,tick))
    p1.start()
    p2.start()

错误

Traceback (most recent call last):
  File "d:\miniconda\lib\multiprocessing\process.py", line 297, in _bootstrap
    self.run()
  File "d:\miniconda\lib\multiprocessing\process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "D:\Program Files (x86)\onedrive\nilm\pi\redd\niubi.py", line 9, in generater
    a.append([random.uniform(1,5),random.uniform(1,5),random.uniform(1,5),random.uniform(1,5)])
  File "<string>", line 2, in append
  File "d:\miniconda\lib\multiprocessing\managers.py", line 792, in _callmethod
    self._connect()
  File "d:\miniconda\lib\multiprocessing\managers.py", line 779, in _connect
    conn = self._Client(self._token.address, authkey=self._authkey)
  File "d:\miniconda\lib\multiprocessing\connection.py", line 490, in Client
    c = PipeClient(address)
  File "d:\miniconda\lib\multiprocessing\connection.py", line 691, in PipeClient
    _winapi.WaitNamedPipe(address, 1000)
FileNotFoundError: [WinError 2] The system cannot find the file specified.

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

有一些要解决的问题,但是主要的问题是您应该包括Process.join,如下所示:

import multiprocessing as mp
import random
import time

...  # generator and printer definitions are unchanged

if __name__=='__main__':
    manager = mp.Manager()  # Create an instance of the manager
    a = manager.list()
    b = manager.list()
    tick = mp.Value('i', 0)
    p1 = mp.Process(target=generator, args=(tick, a, b))
    p2 = mp.Process(target=printer, args=(tick, b))
    p1.start()
    p2.start()
    p1.join()  # Join, to ensure p1 and p2 end
    p2.join()