python - 如何取消使用 `concurrent.futures.ProcessPoolExecutor` 运行的长时间运行的子进程?

标签 python python-multiprocessing python-asyncio

您可以查看完整的here .

我的代码的简化版本如下:

executor = ProcessPoolExecutor(10)
try:
    coro = bot.loop.run_in_executor(executor, processUserInput, userInput)
    result = await asyncio.wait_for(coro, timeout=10.0, loop=bot.loop)
except asyncio.TimeoutError:
    result="Operation took longer than 10 seconds. Aborted."

不幸的是,当操作超时时,即使 future 已被取消,该进程仍在运行。如何取消该进程/任务以使其真正停止运行?

最佳答案

ProcessPoolExecutor 使用multiprocessing 模块。建议使用multiprocessing.Event,而不是取消事件,这不会 .terminate() 子进程。让您的子进程正确退出:

import asyncio
import multiprocessing
import time
from concurrent.futures.process import ProcessPoolExecutor


def f(done):
    print("hi")

    while not done.is_set():
        time.sleep(1)
        print(".")

    print("bye")

    return 12345


async def main():
    done = manager.Event()
    fut = loop.run_in_executor(None, f, done)
    print("waiting...")
    try:
        result = await asyncio.wait_for(asyncio.shield(fut), timeout=3)
    except asyncio.TimeoutError:
        print("timeout, exiting")
        done.set()
        result = await fut
    print("got", result)

loop = asyncio.get_event_loop()
loop.set_default_executor(ProcessPoolExecutor())
manager = multiprocessing.Manager()

loop.run_until_complete(main())

关于python - 如何取消使用 `concurrent.futures.ProcessPoolExecutor` 运行的长时间运行的子进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42325559/

相关文章:

python - 为什么在 `multiprocessing.Pool().apply_async()` 中使用了不止一名 worker ?

python - 使用 Python 多重处理读取大文件

python - asyncio - 有多少协程?

python - 为什么 asyncio.get_event_loop 方法检查当前线程是否为主线程?

python - 解决硬币上的动态规划问题

python - 仅在一个模块中修补方法

python - 无法让我的 Sprite 沿着路径点移动

python - 如何使用最新的 pandas 版本计算扩展协整?

python - 尝试通过运行 Tkinter 的发送进程在进程之间通过管道发送任何内容时发生管道错误

python - 使用任务管理器进行并行计算