【问题标题】:How to cancel long-running subprocesses running using `concurrent.futures.ProcessPoolExecutor`?如何取消使用`concurrent.futures.ProcessPoolExecutor`运行的长时间运行的子进程?
【发布时间】:2017-07-08 14:39:19
【问题描述】:

你可以看到完整的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."

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

【问题讨论】:

    标签: python python-multiprocessing python-asyncio


    【解决方案1】:

    ProcessPoolExecutor 使用multiprocessing 模块。与其取消不.terminate()子进程的事件,不如使用multiprocessing.Event让你的子进程正常退出:

    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())
    

    【讨论】:

    • 9**9**9 不是 time.sleep(1) 怎么取消?
    • 既然这个问题得到了回答和接受,那么发布一个关于您面临的问题的新问题如何?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多