【问题标题】:Is it possible to have always running event loop in ipython similar to how it's done in jupyter notebook?是否可以在 ipython 中始终运行事件循环,类似于在 jupyter notebook 中的操作方式?
【发布时间】:2021-05-14 15:06:50
【问题描述】:

在 Jupyter 中,总是有一个正在运行的事件循环,它允许您在后台运行一些协程的同时交互式地执行代码(例如,使用 asyncio.create_task 启动)。这种行为使测试并发代码变得更加容易。是否有可能在 ipython 中以某种方式实现相同的行为?

我使用 python 3.8 和 ipython 7.12。

【问题讨论】:

  • jupyter 控制台会执行此操作,只需从终端运行 jupyter console 即可。

标签: python python-3.x jupyter-notebook ipython python-asyncio


【解决方案1】:

您可以通过aioconsole 实现此目的。 只需使用 pip 安装即可:

pip3 install aioconsole

然后您可以运行apython 命令来运行交互式异步python shell,您可以在其中运行异步代码。使用示例:

$ apython
Python 3.7.3 (default, Jan 22 2021, 20:04:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
---
This console is running in an asyncio event loop.
It allows you to wait for coroutines using the '{0}' syntax.
Try: await asyncio.sleep(1, result=3)
---
>>> await asyncio.sleep(1, result=3)
3
>>> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 2021-06-17
    • 2014-09-13
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    相关资源
    最近更新 更多