python 文件列表失败_python multiprocessing manager列表错误:[Errno 2]没有这样的文件或目录...

I worte a multiprocessing program in python. I use multiprocessing.Manager().list() to share list within subprocess. At first, I add some tasks in main process. And then, start some subprocesses to do tasks which in the shared list, the subprocesses also add tasks to the shared list. But I got a exception as follow:

Traceback (most recent call last):

File "/usr/lib64/python2.6/multiprocessing/process.py", line 232, in _bootstrap

self.run()

File "/usr/lib64/python2.6/multiprocessing/process.py", line 88, in run

self._target(*self._args, **self._kwargs)

File "gen_friendship.py", line 255, in worker

if tmpu in nodes:

File "", line 2, in __contains__

File "/usr/lib64/python2.6/multiprocessing/managers.py", line 722, in _callmethod

self._connect()

File "/usr/lib64/python2.6/multiprocessing/managers.py", line 709, in _connect

conn = self._Client(self._token.address, authkey=self._authkey)

File "/usr/lib64/python2.6/multiprocessing/connection.py", line 143, in Client

c = SocketClient(address)

File "/usr/lib64/python2.6/multiprocessing/connection.py", line 263, in SocketClient

s.connect(address)

File "", line 1, in connect

error: [Errno 2] No such file or directory

I find something about how to use shared list in python multiprocessing like this. But still have some exception. I have no idea of the meaning of the exception. And what's the difference between the common list and the manager.list?

the code as follow:

nodes = multiprocessing.Manager().list()

lock = multiprocessing.Lock()

AMOUNT_OF_PROCESS = 10

def worker():

lock.acquire()

nodes.append(node)

lock.release()

if __name__ == "__main__":

for i in range(i):

nodes.append({"name":"username", "group":1})

processes = [None for i in range(AMOUNT_OF_PROCESS)]

for i in range(AMOUNT_OF_PROCESS):

processes[i] = multiprocessing.Process(taget=worker, args=())

processes[i].start()

解决方案

The problem is that your main process is exiting immediately after you start all your worker processes, which shuts down your Manager. When your Manager shuts down, none of the children can use the shared list you passed into them. You can fix it by using join to wait for all the children to finish. Just make sure you actually start all your processes prior to calling join:

for i in range(AMOUNT_OF_PROCESS):

processes[i] = multiprocessing.Process(taget=worker, args=())

processes[i].start()

for process in processes:

process.join()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值