将 collections.namedtuple 与 ProcessPoolExecutor 一起使用在某些情况下会陷入困境

2024-04-17

>>> import concurrent.futures
>>> from collections import namedtuple
>>> #1. Initialise namedtuple here
>>> # tm = namedtuple("tm", ["pk"])  
>>> class T:  
...     #2. Initialise named tuple here
...     #tm = namedtuple("tm", ["pk"]) 
...     def __init__(self): 
...         #3: Initialise named tuple here
...         tm = namedtuple("tm", ["pk"])                       
...         self.x = {'key': [tm('value')]}  
...     def test1(self):  
...         with concurrent.futures.ProcessPoolExecutor(max_workers=1) as executor:  
...             results = executor.map(self.test, ["key"])  
...         return results  
...     def test(self, s): 
...         print(self.x[s])   
... 
>>> t = T().test1()

这就卡在这里了。

^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
Process ForkProcess-1:
  File "<stdin>", line 10, in test1
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 623, in __exit__
    self.shutdown(wait=True)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/process.py", line 681, in shutdown
    self._queue_management_thread.join()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 1044, in join
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/process.py", line 233, in _process_worker
    call_item = call_queue.get(block=True)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/queues.py", line 94, in get
    res = self._recv_bytes()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/connection.py", line 216, in recv_bytes
    buf = self._recv_bytes(maxlength)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/connection.py", line 407, in _recv_bytes
    buf = self._recv(4)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/connection.py", line 379, in _recv
    chunk = read(handle, remaining)
KeyboardInterrupt
    self._wait_for_tstate_lock()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 1060, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt

如果我在类之外初始化命名元组(在 #1 中),在这种情况下,效果很好。如果我按照 #2 或 #3 初始化,有人可以告诉我问题是什么吗?


您没有更改初始化命名元组的位置。您正在更改创建namedtuple的位置class.

当您在模块“y”中创建名为“x”的namedtuple类时collections.namedtuple, its __module__被设定为'y'和它的__qualname__被设定为'x'。酸洗和反酸洗依赖于该类实际上在y.x这些属性指示的位置,但在示例的情况 2 和 3 中,情况并非如此。

Python 无法 pickle 命名元组,这会破坏与工作进程的进程间通信。执行中self.test在工作进程中依赖于酸洗self.test并在工作进程中取消它的副本,如果self.x是无法腌制的类的实例。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 collections.namedtuple 与 ProcessPoolExecutor 一起使用在某些情况下会陷入困境 的相关文章

随机推荐