你可以看到完整的here https://github.com/traverseda/rratbot/blob/master/main.py#L35.
我的代码的简化版本如下:
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."
不幸的是,当操作超时时,即使未来已被取消,该进程仍在运行。如何取消该进程/任务以使其真正停止运行?
ProcessPoolExecutor
使用multiprocessing
模块。而不是取消活动,这不会.terminate()
子进程,建议使用multiprocessing.Event https://docs.python.org/3.6/library/multiprocessing.html#synchronization-primitives让您的子进程正确退出:
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())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)