Flask 在restful接口中运行_forever()?如何在多个restful调用上保持asyncio循环活动

Flask 在restful接口中运行_forever()?如何在多个restful调用上保持asyncio循环活动,flask,websocket,python-asyncio,flask-restful,Flask,Websocket,Python Asyncio,Flask Restful,有没有办法在flask中的restful接口调用之间保持asyncio循环的活动状态 在我的例子中,flask中有一个restful接口,它使用asyncio访问另一个websocket。我希望避免每次进行restful调用时都重新实例化asyncio外观 有没有办法让它在多个restful调用之间保持活动状态?是的,请尝试Python库线程化。比如: from threading import Thread, Event class AsyncIO(Thread): def run(

有没有办法在flask中的restful接口调用之间保持asyncio循环的活动状态

在我的例子中,flask中有一个restful接口,它使用asyncio访问另一个websocket。我希望避免每次进行restful调用时都重新实例化asyncio外观


有没有办法让它在多个restful调用之间保持活动状态?

是的,请尝试Python库
线程化
。比如:

from threading import Thread, Event

class AsyncIO(Thread):
    def run(self):
        something.loop_forever()

asyncio_thread = Thread()
asyncio_stop = Event()

if not asyncio_thread.is_alive():
    asyncio_thread = AsyncIO()
    asyncio_thread.start()

# The code is executed here and AsyncIO is running

别忘了用AsyncIO的参考取代
一些东西
,这是一个非常有趣的想法。很少有评论。是否应该是
什么。永远运行(
?到底什么东西?它是
asyncio.set\u event\u loop(asyncio.new\u event\u loop())
然后
something=asyncio.get\u event\u loop()
。然后我可以全局检索该循环并将不同的协程连接到它吗?我在这里添加了更多细节: