- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何终止 loop.run_in_executor
与 ProcessPoolExecutor
优雅?启动程序后不久,发送 SIGINT (ctrl + c)。
def blocking_task():
sleep(3)
async def main():
exe = concurrent.futures.ProcessPoolExecutor(max_workers=4)
loop = asyncio.get_event_loop()
tasks = [loop.run_in_executor(exe, blocking_task) for i in range(3)]
await asyncio.gather(*tasks)
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print('ctrl + c')
与
max_workers
等于或小于一切正常的任务数。但如果
max_workers
越大,上面代码的输出如下:
Process ForkProcess-4:
Traceback (most recent call last):
File "/usr/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "/usr/lib/python3.8/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/usr/lib/python3.8/concurrent/futures/process.py", line 233, in _process_worker
call_item = call_queue.get(block=True)
File "/usr/lib/python3.8/multiprocessing/queues.py", line 97, in get
res = self._recv_bytes()
File "/usr/lib/python3.8/multiprocessing/connection.py", line 216, in recv_bytes
buf = self._recv_bytes(maxlength)
File "/usr/lib/python3.8/multiprocessing/connection.py", line 414, in _recv_bytes
buf = self._recv(4)
File "/usr/lib/python3.8/multiprocessing/connection.py", line 379, in _recv
chunk = read(handle, remaining)
KeyboardInterrupt
ctrl + c
我想只捕获一次异常(KeyboardInterrupt)并忽略或静音进程池中的其他异常,但是如何?
最佳答案
您可以使用 initializer
parameter的 ProcessPoolExecutor
为 SIGINT
安装处理程序在每个过程中。
更新:
在 Unix 上,当进程被创建时,它成为其父进程组的成员。如果您正在生成 SIGINT
与 Ctrl+C
,则信号被发送到整个进程组。
import asyncio
import concurrent.futures
import os
import signal
import sys
from time import sleep
def handler(signum, frame):
print('SIGINT for PID=', os.getpid())
sys.exit(0)
def init():
signal.signal(signal.SIGINT, handler)
def blocking_task():
sleep(15)
async def main():
exe = concurrent.futures.ProcessPoolExecutor(max_workers=5, initializer=init)
loop = asyncio.get_event_loop()
tasks = [loop.run_in_executor(exe, blocking_task) for i in range(2)]
await asyncio.gather(*tasks)
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print('ctrl + c')
Ctrl-C
开始后不久:
^CSIGINT for PID= 59942
SIGINT for PID= 59943
SIGINT for PID= 59941
SIGINT for PID= 59945
SIGINT for PID= 59944
ctrl + c
关于python - 如何使用 ProcessPoolExecutor 优雅地终止 loop.run_in_executor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63729195/
我如何设置一个阻塞函数在执行器中运行,以一种结果无关紧要的方式,这样主线程不应该等待或被它减慢。 老实说,我不确定这是否是正确的解决方案,我想要的只是将某种类型的处理队列与主进程分开,这样它就不会阻止
我觉得 run_in_executor() asyncio的方法图书馆属于loop目的。 特别是,如果我选择以“通常”的方式在异步事件循环旁边运行第二个线程,会有什么不同,作者是 import thr
概览 我正在尝试并行化一个文本分类项目,该项目肯定需要很长时间才能完全串行运行。我已经尝试了这两种可能的变体,我相信它们的功能相似,并且对我在资源监视器中看到的每种结果很好奇。 第一个解决方案 我尝试
我有一个服务器应用程序,当客户请求时,我会安排一些工作,比如 def work(): time.sleep(5) fut = asyncio.get_event_loop().run_in_e
如何在使用 run_in_executor 调用的 run_long_thing() 函数中引发异常?看起来像是被吞噬了一样。我不需要阻塞代码中函数的结果。它基本上是一个即发即忘的功能,但如果有任何异
如何终止 loop.run_in_executor与 ProcessPoolExecutor优雅?启动程序后不久,发送 SIGINT (ctrl + c)。 def blocking_task():
全部! 我需要向 Web 服务发出大约 10,000 个请求,并且我希望得到 JSON 响应。由于请求是相互独立的,所以我想并行运行它们。我认为 aiohttp 可以帮助我解决这个问题。我编写了以下代
我想运行一个使用协程和多线程请求 URL 的服务。但是我无法将协程传递给执行器中的工作人员。有关此问题的最小示例,请参阅下面的代码: import time import asyncio import
我正在尝试在 concurrent.futures 的帮助下同时发送 POST 请求。由于某种原因,我无法设置自定义 header 。我要设置 授权 内容类型 这是我到目前为止所取得的进展。 impo
我正在使用 asyncio 来运行一段这样的阻塞代码: result = await loop.run_in_executor(None, long_running_function) 我的问题是:我
我正在尝试将参数传递给 run_in_executor,如下所示: loop.run_in_executor(None, update_contacts, data={ 'em
我正在试用 asyncio,并且必须将它与一些普通的多线程阻塞代码混合使用,因此我需要使用 run_in_exector 卸载执行。 asyncio docs warn that "most func
我对 Python Tornado 很陌生,并且一直在尝试启动一个新线程来运行一些 IO 阻塞代码,同时允许服务器继续处理新请求。我一直在阅读,但似乎仍然无法弄清楚这两个功能之间有什么区别? 例如调用
我编写了一个基准实用程序来批量查询 REST 端点。它通过三种方式实现: 依次使用请求库, 同时使用请求库,但使用 loop.run_in_executor() 包装每个请求, 同时使用 aiohtt
我指的是this repo是为了让mmaction2 grad-cam demo从短视频离线推理适配到长视频在线推理。脚本如下所示: 注意:为了使这个脚本可以很容易地重现,我注释掉了一些需要很多依赖的
问题重现环境: 操作系统:Windows 10(主机) 中央处理器:8 python :3.6.6 游戏版本:1.9.4 “构建器”:cx_Freeze 版本 5.1.1 问题未重现的环境: 操作系统
我正在用 Python 试验我的第一个小型爬虫,我想使用 asyncio 同时获取多个网站。我已经编写了一个与 aiohttp 一起使用的函数,但是由于 aiohttp.request() 不执行 j
我正在通过默认的 asyncio 事件循环运行函数 provision_ec2_node() thread executor .该函数采用一些参数,我通过 functools.partial() 将这
我是一名优秀的程序员,十分优秀!