是否有一种方法可以使烧瓶中的宁静接口调用之间保持异步循环活跃?
就我而言,我在flask中有一个宁静的接口,该接口使用asyncio访问另一个websocket。我想避免每次进行宁静调用时都会重新实例化异步外观。
是否有办法使它在多个静息呼叫之间保持活动状态?
是,请尝试使用Python库threading
。类似于:
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参考取代something