Python 如何重新启动异步IO线程?

Python 如何重新启动异步IO线程?,python,asynchronous,python-asyncio,Python,Asynchronous,Python Asyncio,如何重新启动异步IO循环?我正在用asyncio听websocket。我想停止监听,重新启动整个循环。我该怎么做?我下面的尝试不起作用 async def start_websocket(streams): print("using streams {}".format(streams)) await asyncio.sleep(30) def _start_loop(loop, ws): asyncio.set_event_loop(loop) try:

如何重新启动异步IO循环?我正在用asyncio听websocket。我想停止监听,重新启动整个循环。我该怎么做?我下面的尝试不起作用

async def start_websocket(streams):
    print("using streams {}".format(streams))
    await asyncio.sleep(30)


def _start_loop(loop, ws):
    asyncio.set_event_loop(loop)
    try:
        loop.run_until_complete(ws)
    except CancelledError:
        pass


for streams in ["a", "b"]:
    ws = start_websocket(streams)  # coroutine
    loop = asyncio.get_event_loop()

    # in case of already running, cancel websocket
    if loop.is_running():
        [t.cancel() for t in asyncio.Task.all_tasks()]

    # restart websocket
    Thread(target=_start_loop, args=(loop, ws)).start()
    time.sleep(2)
我要走了

RuntimeError: This event loop is already running

您可以潜在地停止循环(
loop.stop()
loop.close()
),并通过
loop=asyncio.new\u event\u loop()


另一个选项是创建自定义事件循环策略:

您可以潜在地停止循环(
loop.stop()
loop.close()
),并通过
loop=asyncio.new\u event\u loop()

另一个选项是创建自定义事件循环策略:

这将修复您以前遇到的错误


async def subtask(s):
    while True:
        print("running task {}".format(s))
        await asyncio.sleep(5)

async def start_websocket(streams):
    print("using streams {}".format(streams))
    asyncio.ensure_future(subtask(streams+"1"))
    asyncio.ensure_future(subtask(streams+"2"))
    try:
        while True:
            print("running task {}".format(streams))
            await asyncio.sleep(5)
    except CancelledError:
        print("cancelled task {}".format(streams))

def _start_loop(loop):

    asyncio.set_event_loop(loop)
    loop.run_forever()


for streams in ["a", "b", "c"]:
    loop = asyncio.get_event_loop()
    if not loop.is_running():
        Thread(target=_start_loop, args=(loop,)).start()
    else:
        for t in asyncio.Task.all_tasks():
            t.cancel()

    loop.create_task(start_websocket(streams))
    time.sleep(10)
    print("finished {}".format(streams))

time.sleep(60)

这将修复您以前遇到的错误

我已使示例自包含。你有一些示例代码吗?我已经使示例自包含。您有一些示例代码吗?谢谢,这给出了错误:RuntimeError:Event loop is Closed谢谢这给出了错误:RuntimeError:Event loop is Closed您能添加一些关于原始代码和新版本之间差异的解释性文本吗,为了让未来的读者能够从中学习,您能否添加一些关于原始代码和新版本之间差异的解释性文本,以便未来的读者能够从中学习?

async def subtask(s):
    while True:
        print("running task {}".format(s))
        await asyncio.sleep(5)

async def start_websocket(streams):
    print("using streams {}".format(streams))
    asyncio.ensure_future(subtask(streams+"1"))
    asyncio.ensure_future(subtask(streams+"2"))
    try:
        while True:
            print("running task {}".format(streams))
            await asyncio.sleep(5)
    except CancelledError:
        print("cancelled task {}".format(streams))

def _start_loop(loop):

    asyncio.set_event_loop(loop)
    loop.run_forever()


for streams in ["a", "b", "c"]:
    loop = asyncio.get_event_loop()
    if not loop.is_running():
        Thread(target=_start_loop, args=(loop,)).start()
    else:
        for t in asyncio.Task.all_tasks():
            t.cancel()

    loop.create_task(start_websocket(streams))
    time.sleep(10)
    print("finished {}".format(streams))

time.sleep(60)