[英]Shutting down manager error "AttributeError: 'ForkAwareLocal' object has no attribute 'connection'" when using namespace and shared memory dict
[英]Sharing a list between processes raises the error: AttributeError: 'ForkAwareLocal' object has no attribute 'connection' when I try to use the list
我正在嘗試將列表傳遞給運行 function 以比較 colors 的進程。 然后我希望這些 colors 返回到列表中,以便我可以在進程之外的 function 中使用它。 我怎樣才能做到這一點?
到目前為止,我所擁有的只是一個縮小的問題,然后我將其簡化為示例:
import multiprocessing
def test(c):
c[0] = 1
class TestClass:
def __init__(self):
with multiprocessing.Manager() as manager:
colorcodes = manager.list()
p = multiprocessing.Process(target=test, args=(colorcodes,))
p.start()
p.join()
print(colorcodes[0])
if __name__ == '__main__':
TestClass()
返回:
Process Process-2:
Traceback (most recent call last):
File "C:\Users\usr\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap
self.run()
File "C:\Users\usr\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "D:\Python Development\example_env\example.py", line 5, in test
c[0] = 1
File "<string>", line 2, in __setitem__
File "C:\Users\usr\AppData\Local\Programs\Python\Python37\lib\multiprocessing\managers.py", line 834, in _callmethod
raise convert_to_error(kind, result)
IndexError: list assignment index out of range
Traceback (most recent call last):
File "C:\Users\usr\AppData\Local\Programs\Python\Python37\lib\multiprocessing\managers.py", line 811, in _callmethod
conn = self._tls.connection
AttributeError: 'ForkAwareLocal' object has no attribute 'connection'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "example.py", line 19, in <module>
TestClass()
File "example.py", line 15, in __init__
print(colorcodes[0])
File "<string>", line 2, in __getitem__
File "C:\Users\usr\AppData\Local\Programs\Python\Python37\lib\multiprocessing\managers.py", line 815, in _callmethod
self._connect()
File "C:\Users\usr\AppData\Local\Programs\Python\Python37\lib\multiprocessing\managers.py", line 802, in _connect
conn = self._Client(self._token.address, authkey=self._authkey)
File "C:\Users\usr\AppData\Local\Programs\Python\Python37\lib\multiprocessing\connection.py", line 490, in Client
c = PipeClient(address)
File "C:\Users\usr\AppData\Local\Programs\Python\Python37\lib\multiprocessing\connection.py", line 691, in PipeClient
_winapi.WaitNamedPipe(address, 1000)
FileNotFoundError: [WinError 2] The system cannot find the file specified
我在 Windows 10 Home 64-bit v. 1909 上使用 Python 版本 3.7.6
您正在創建一個列表並嘗試訪問超出范圍的索引。 在您的 function 測試中,您必須使用 append 將項目添加到列表的索引 [0]。
您正在嘗試打印在“with 語句”內部但在其外部聲明的變量(顏色代碼列表)
此修改后的代碼應該可以工作:
import multiprocessing
def test(c):
c.append(1)
class TestClass:
def __init__(self):
with multiprocessing.Manager() as manager:
colorcodes = manager.list()
p = multiprocessing.Process(target=test, args=(colorcodes,))
p.start()
p.join()
print(colorcodes[0])
if __name__ == '__main__':
TestClass()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.