問題描述
我在 python 中編寫了一個多處理程序.我使用 multiprocessing.Manager().list()
在子進程中共享列表.首先,我在主流程中添加了一些任務(wù).然后,啟動一些子進程來執(zhí)行共享列表中的任務(wù),子進程也將任務(wù)添加到共享列表中.但我得到了一個例外:
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 "<string>", 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 "<string>", line 1, in connect
error: [Errno 2] No such file or directory
我發(fā)現(xiàn)了一些關(guān)于如何在 python 多處理中使用共享列表的信息,例如 這個.但還是有一些例外.我不知道異常的含義.而common list和manager.list有什么區(qū)別?
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?
代碼如下:
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()
推薦答案
問題是你的主進程在你啟動所有工作進程后立即退出,這會關(guān)閉你的 Manager
.當(dāng)您的 Manager
關(guān)閉時,沒有一個孩子可以使用您傳遞給他們的共享列表.您可以通過使用 join
等待所有孩子完成來修復(fù)它.只需確保在調(diào)用 join
之前確實 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(target=worker, args=())
processes[i].start()
for process in processes:
process.join()
這篇關(guān)于python多處理管理器列表錯誤:[Errno 2]沒有這樣的文件或目錄的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!