Flask应用程序中的asyncio event_循环

2024-05-10 19:52:23 发布

您现在位置:Python中文网/ 问答频道 /正文

在Flask应用程序中运行异步事件循环的最佳方法是什么?在

我的主.py看起来像这样:

if __name__ == '__main__':
    try:
        app.run(host='0.0.0.0', port=8000, debug=True)
    except:
        logging.critical('server: CRASHED: Got exception on main handler')
        logging.critical(traceback.format_exc())
        raise

为了添加异步任务选项,我需要在运行app之前创建一个event_loop,但是即使停止应用程序运行,后台线程仍然挂起(在调试器中可以观察到)

^{pr2}$

并使用以下命令创建异步任务:

def future_callback(fut):
    if fut.exception():
        logging.error(fut.exception())

def fire_and_forget(func, *args, **kwargs):
    if callable(func):
        future = app.event_loop.run_in_executor(None, func, *args, **kwargs)
        future.add_done_callback(future_callback)
    else:
        raise TypeError('Task must be a callable')

我能找到的唯一解决方案是在finally块的末尾添加exit(),但我认为这不是正确的解决方案。在


Tags: runloopeventappifmainloggingdef
2条回答

我最初对整个flask应用程序采用一个event_循环的方法,但是由于一些不同的原因,我不喜欢这样。在

相反,我创建了一个helper文件custom_flask_async.py,其中包含2个函数

import asyncio


def get_set_event_loop():
    try:
        return asyncio.get_event_loop()
    except RuntimeError as e:
        if e.args[0].startswith('There is no current event loop'):
            asyncio.set_event_loop(asyncio.new_event_loop())
            return asyncio.get_event_loop()
        raise e


def run_until_complete(tasks):
    return get_set_event_loop().run_until_complete(asyncio.gather(*tasks))

我只检查和get_set事件循环,如果它将被使用,并在特定的请求中等待。因为我使用这个的主要方法是使用带有聚集的run_until_complete,所以我也在helper中使用了它。在

我也不确定这是不是最好的方法,或者这种方法有什么缺点,但它绝对适合我的目的。在

最终,我找到的最好的解决方案是将我的Flask应用程序托管在uwsgi中,这样可以使用mules和{}来执行异步任务

https://uwsgi-docs.readthedocs.io/en/latest/Spooler.html

https://uwsgi-docs.readthedocs.io/en/latest/Mules.html

相关问题 更多 >