loop.run_forever()在一个宁静的界面中?如何通过多个宁静的调用使asyncio循环保持活动状态

问题描述 投票:0回答:1

是否有一种方法可以使烧瓶中的宁静接口调用之间保持异步循环活跃?

就我而言,我在flask中有一个宁静的接口,该接口使用asyncio访问另一个websocket。我想避免每次进行宁静调用时都会重新实例化异步外观。

是否有办法使它在多个静息呼叫之间保持活动状态?

flask websocket python-asyncio flask-restful
1个回答
0
投票

是,请尝试使用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

© www.soinside.com 2019 - 2024. All rights reserved.