我的webapp使用aiohttp,并在不同的线程上执行分析。我将join()
退出时的所有执行器线程。executor.shutdown(wait=True)
如果线程是在协同程序中启动的,则似乎不起作用。在
这不是我真正的代码,而是一个示例:
import asyncio
from concurrent.futures import ThreadPoolExecutor
import atexit
import signal
import time
import os
def atexitHandler():
try:
executor.shutdown(wait=True)
except Exception:
pass
for task in asyncio.Task.all_tasks():
try:
loop.call_soon_threadsafe(task.cancel)
except Exception:
pass
try:
loop.stop()
except Exception:
pass
print("'Bye")
def analysis():
print("analysis started")
time.sleep(10)
print("analysis finished")
@asyncio.coroutine
def analysis_coro(loop, executor):
loop.call_soon_threadsafe(loop.run_in_executor, executor, analysis)
loop = asyncio.get_event_loop()
loop.add_signal_handler(
signal.SIGTERM | signal.SIGINT | signal.SIGQUIT,
atexitHandler
)
cores_num = os.cpu_count() or 1
executor = executor = ThreadPoolExecutor(max_workers=cores_num)
try:
loop.run_until_complete(analysis_coro(loop, executor))
except keyboardInterrupt:
atexitHandler()
相反,如果我直接运行loop.call_soon_threadsafe(loop.run_in_executor, executor, analysis)
并执行loop.run_forever()
,它可以正常工作。在
PS:由于CentOS 6.5,我被Python3.4卡住了(我不能使用编译版本)
目前没有回答
相关问题 更多 >
编程相关推荐