asyncio.run() cannot be called from a running event loop 解决方案

问题发生

在jupyter notebook 中运行异步编程

import asyncio

async def crawl_page(url):
    print('crawling {}'.format(url))
    sleep_time = int(url.split('_')[-1])
    await asyncio.sleep(sleep_time)
    print('OK {}'.format(url))

async def main(urls):
    for url in urls:
        await crawl_page(url)
%time asyncio.run(main(['url_1', 'url_2', 'url_3', 'url_4']))

出现错误

asyncio.run() cannot be called from a running event loop

解决办法

pip3 install tornado==4.5.3 --user

你可能感兴趣的:(asyncio.run() cannot be called from a running event loop 解决方案)