我试图将一个列表传递给一个进程,该进程运行一个函数来比较颜色。然后我希望这些颜色返回到列表中,这样我就可以在进程之外的函数中使用它。我该怎么做
到目前为止,我所拥有的只是一个缩小的问题,然后我将其简化为示例:
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位v上使用Python版本3.7.6。1909年
您正在创建列表并试图访问超出范围的索引。在函数测试中,必须使用append将项添加到列表的索引[0]
您正试图打印一个变量(颜色代码列表),该变量在“with statement”内部声明,但在其外部声明
修改后的代码应该可以工作:
相关问题 更多 >
编程相关推荐