python - 在 Python 中使用进程共享对象时收到 SIGINT 后如何防止 BrokenPipeErrors?
问题描述
这个 Python 程序:
import concurrent.futures
import multiprocessing
import time
class A:
def __init__(self):
self.event = multiprocessing.Manager().Event()
def start(self):
try:
while True:
if self.event.is_set():
break
print("processing")
time.sleep(1)
except BaseException as e:
print(type(e).__name__ + " (from pool thread):", e)
def shutdown(self):
self.event.set()
if __name__ == "__main__":
try:
a = A()
pool = concurrent.futures.ThreadPoolExecutor(1)
future = pool.submit(a.start)
while not future.done():
concurrent.futures.wait([future], timeout=0.1)
except BaseException as e:
print(type(e).__name__ + " (from main thread):", e)
finally:
a.shutdown()
pool.shutdown()
输出:
processing
processing
processing
KeyboardInterrupt (from main thread):
BrokenPipeError (from pool thread): [WinError 232] The pipe is being closed
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\multiprocessing\managers.py", line 788, 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 ".\foo.py", line 34, in <module>
a.shutdown()
File ".\foo.py", line 21, in shutdown
self.event.set()
File "C:\Program Files\Python37\lib\multiprocessing\managers.py", line 1067, in set
return self._callmethod('set')
File "C:\Program Files\Python37\lib\multiprocessing\managers.py", line 792, in _callmethod
self._connect()
File "C:\Program Files\Python37\lib\multiprocessing\managers.py", line 779, in _connect
conn = self._Client(self._token.address, authkey=self._authkey)
File "C:\Program Files\Python37\lib\multiprocessing\connection.py", line 490, in Client
c = PipeClient(address)
File "C:\Program Files\Python37\lib\multiprocessing\connection.py", line 691, in PipeClient
_winapi.WaitNamedPipe(address, 1000)
FileNotFoundError: [WinError 2] The system cannot find the file specified
当它运行并SIGINT
在三秒后发送信号时(按Ctrl+ C)。
分析。—SIGINT
信号被发送到每个进程的主线程。在这种情况下,有两个进程:主进程和管理器的子进程。
- 在主进程的主线程中:接收到
SIGINT
信号后,默认的SIGINT
信号处理程序引发KeyboardInterrupt
异常,被捕获并打印。 - 在管理器的子进程的主线程中:同时,
SIGINT
默认的SIGINT
信号处理程序在接收到信号后,会引发KeyboardInterrupt
异常,从而终止子进程。因此,其他进程对管理器共享对象的所有后续使用都会引发BrokenPipeError
异常。 - 在主进程的池子线程中:在这种情况下,
BrokenPipeError
在行引发异常if self.event.is_set():
。 - 在主进程的主线程中:最后,控制流到达行
a.shutdown()
,这引发了AttributeError
和FileNotFoundError
异常。
如何防止这种BrokenPipeError
异常?
解决方案
此问题的解决方案是SIGINT
使用将忽略信号的处理程序覆盖默认信号处理程序,例如使用signal.SIG_IGN
标准信号处理程序。可以通过signal.signal
在管理器的子进程开始时调用该函数来实现:
import concurrent.futures
import multiprocessing.managers
import signal
import time
def init():
signal.signal(signal.SIGINT, signal.SIG_IGN)
class A:
def __init__(self):
manager = multiprocessing.managers.SyncManager()
manager.start(init)
self.event = manager.Event()
def start(self):
try:
while True:
if self.event.is_set():
break
print("processing")
time.sleep(1)
except BaseException as e:
print(type(e).__name__ + " (from pool thread):", e)
def shutdown(self):
self.event.set()
if __name__ == "__main__":
try:
a = A()
pool = concurrent.futures.ThreadPoolExecutor(1)
future = pool.submit(a.start)
while not future.done():
concurrent.futures.wait([future], timeout=0.1)
except BaseException as e:
print(type(e).__name__ + " (from main thread):", e)
finally:
a.shutdown()
pool.shutdown()
笔记。— 该程序也适用于concurrent.futures.ProcessPoolExecutor
.
推荐阅读
- python - Python Loggin - 仅记录到文件
- python - 如何将特定的 CURL 请求转换为 Python
- python - 无法使用 selenium webdriver 在 google colab 中打开网站
- php - 如何使用 PHP 将数据数组插入数据库?
- python - Else 语句继续执行 PYTHON
- apache-spark - Spark Databricks 对镶木地板文件的超慢读取
- c# - 通过 .NET 5 Web API 中的外键获取另一个表数据时在返回时引发异常
- reactjs - 异步/等待在 createAsyncThunk 中定义的 for-of 循环中不起作用
- sms - Okta mfa 短信模板不支持“@”字符
- r - R 中的错误 - t(XXsample) %*% XXsample 中的错误:需要数字/复矩阵/向量参数