run Python asyncio code in a Jupyter notebook

 

New Jupyter lab/notebook

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import asyncio
import time
 
async def my_coroutine(): 
    await asyncio.sleep(1
    print("Coroutine executed!")
 
s = time.perf_counter()
loop = asyncio.get_event_loop()
loop.create_task(my_coroutine())
asyncio.run_coroutine_threadsafe(my_coroutine(), loop)
asyncio.gather(my_coroutine())
elapsed = time.perf_counter() - s
print(f"Script executed in {elapsed:0.2f} seconds.")

  

Modern Jupyter lab/notebook

1
2
3
async def main():
    print(1
await main()

  

posted @   zyip  阅读(80)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2014-04-03 C# return dynamic/anonymous type value as function result
2014-04-03 微信模拟登录 获取appkey 设置token
点击右上角即可分享
微信分享提示