简体   繁体   English

Jupyter笔记本运行等待功能

[英]Jupyter notebook to run await function

Upon learning Coroutines and Tasks on Jupyter notebook , 在学习Jupyter笔记本上的协同程序和任务时,

Run the following codes 运行以下代码

import asyncio
async def main():
    print('learn')
    await asyncio.sleep(1)
    print('Jupyter')

在此输入图像描述

However, it works properly on Ipython 但是,它在Ipython上运行正常

在此输入图像描述

This is a known issue with later versions of Jupyter. 这是Jupyter更高版本的已知问题 Install nest_asyncio as a workaround . 安装nest_asyncio作为解决方法

> pip install nest_asyncio

Code

import asyncio

import nest_asyncio


nest_asyncio.apply()


async def main():
    print("Learn")
    await asyncio.sleep(1)
    print("Jupyter")


asyncio.run(main())
# 'Learn'
# 'Jupyter'

TLDR ; TLDR ; Running asyncio in notebooks conflicts with the existing event loop run by Tornado 5.0 in the background. 在笔记本中运行asyncio与Tornado 5.0在后台运行的现有事件循环冲突。 A second option is to downgrade notebook to a version that depends on an older version of Tornado. 第二种选择是将notebook降级到依赖于旧版Tornado的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM