<p><strong>编辑2019年2月21日:问题已解决</strong></p>
<blockquote>
<p>This is no longer an issue on the latest version of Jupyter Notebook. Authors of Jupyter Notebook detailed the case <a href="https://blog.jupyter.org/ipython-7-0-async-repl-a35ce050f7f7" rel="noreferrer">here</a>.</p>
</blockquote>
<p>下面的答案是op标记正确的原始响应</p>
<hr/>
<p>这是很久以前发布的,但如果其他人正在寻找一个解释和解决的问题运行异步代码在Jupyter笔记本</p>
<p>Jupyter的Tornado 5.0在添加了自己的asyncio事件循环之后更新了砖砌异步功能:</p>
<p><a href="https://i.stack.imgur.com/TRRbg.png" rel="noreferrer"><img src="https://i.stack.imgur.com/TRRbg.png" alt="Terminal output of <code>get_event_loop()</code>"/></a>
<a href="https://i.stack.imgur.com/5TmGl.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5TmGl.png" alt="Jupyter Notebook output of <code>get_event_loop()</code>"/></a></p>
<p>因此,对于要在Jupyter笔记本上运行的任何异步功能,您不能调用<code>run_until_complete()</code>,因为您将从<code>asyncio.get_event_loop()</code>接收的循环将处于活动状态。</p>
<p>相反,必须将任务添加到当前循环:</p>
<pre><code>import asyncio
loop = asyncio.get_event_loop()
loop.create_task(some_async_function())
</code></pre>
<p>Jupyter笔记本上运行的一个简单示例:</p>
<p><a href="https://i.stack.imgur.com/erbUY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/erbUY.png" alt="enter image description here"/></a></p>