线程之间共享列表的替代方法(多处理)

时间:2019-07-08 09:00:13

标签: python multiprocessing

我需要从多处理库产生的进程中访问列表的元素。但是,我已经读到,在进程之间具有共享的数据结构是一个坏主意,但是我不确定该如何解决这个问题。该列表包含计数器变量,每个变量都需要由进程访问。列表中的计数器变量需要通过每个进程内部的循环来递增。我正在寻找一种在流程之间创建共享列表的替代方法。我尝试在下面的代码所示的进程之间创建共享列表失败。任何产生错误的替代方法/辅助方法将不胜感激。

with Manager() as manager:
    aout_stable = manager.list([0]*len(aout))

def calculate(ain_index_):
    ainn= ain[ain_index_]
    for parameters in itertools.product(aout,qoutlist,samples_eccentricities):
        #Finding index for which counter variable to increment within list
        aout_index = np.where(aout==parameters[0])[0][0]
        a_ratio = parameters[0]/ainn
        inclination_limit = (math.pi/0.3)*(1-a_ratio/((( (1+parameters[1])*(1+parameters[2])/(np.sqrt(1-parameters[2]) ))**(2./5.) )*(2.8/(1-parameters[2]))))
        stable_num = len(np.where(inclinations>inclination_limit)[0])
        aout_stable[aout_index] += stable_num

pool = multiprocessing.Pool(processes=20)
ain_index = [i for i in range(len(ain))]
result = pool.map(calculate,ain_index)

不成功的代码返回的错误:

Traceback (most recent call last):
  File "itertools test.py", line 82, in <module>
    result = pool.map(calculate,ain_index)
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 251, in map
    return self.map_async(func, iterable, chunksize).get()
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 558, in get
    raise self._value
socket.error: [Errno 2] No such file or directory

仅在ain数组的一个元素上而不是在整个数组上运行calculate函数会产生以下错误:

  File "itertools test.py", line 80, in <module>
    x = calculate(1)
  File "itertools test.py", line 71, in calculate
    aout_stable[aout_index] += stable_num
  File "<string>", line 2, in __getitem__
  File "/usr/lib/python2.7/multiprocessing/managers.py", line 755, in _callmethod
    self._connect()
  File "/usr/lib/python2.7/multiprocessing/managers.py", line 742, in _connect
    conn = self._Client(self._token.address, authkey=self._authkey)
  File "/usr/lib/python2.7/multiprocessing/connection.py", line 169, in Client
    c = SocketClient(address)
  File "/usr/lib/python2.7/multiprocessing/connection.py", line 304, in SocketClient
    s.connect(address)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 2] No such file or directory

0 个答案:

没有答案