簡體   English   中英

是否可以在 ipython 中始終運行事件循環,類似於在 jupyter notebook 中的操作方式?

[英]Is it possible to have always running event loop in ipython similar to how it's done in jupyter notebook?

在 Jupyter 中,總是有一個正在運行的事件循環,它允許您在后台運行一些協程(例如,使用asyncio.create_task啟動)時交互式地執行代碼。 這種行為使測試並發代碼變得更加容易。 是否有可能在 ipython 中以某種方式實現相同的行為?

我使用 python 3.8 和 ipython 7.12。

您可以使用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
>>> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM