当前位置:网站首页>Use anyio instead of asyncio
Use anyio instead of asyncio
2022-07-29 11:14:00 【waketzheng】
1. gather
Example:
import anyio
async def gather(*coros):
results = [None] * len(coros)
async def runner(coro, i):
results[i] = await coro
async with anyio.create_task_group() as tg:
for i, coro in enumerate(coros):
tg.start_soon(runner, coro, i)
return results
async def foo(i):
print(i)
async def main():
await gather(*[foo(i) for i in range(5)])
if __name__ == "__main__":
anyio.run(main)2. run_until_complete
asyncio:
import asyncio
from typing import Coroutine
from tortoise import Toirtoise
def run_async(coro: Coroutine) -> None:
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(coro)
finally:
loop.run_until_complete(Tortoise.close_connections())--> anyio:
import anyio
from typing import Coroutine
from tortoise import Toirtoise
def run_async(coro: Coroutine) -> None:
async def runner():
try:
await coro
finally:
await Tortoise.close_connections()
anyio.run(runner)
边栏推荐
- TCP and UDP
- Determine whether the values of two objects are equal
- 深入理解C# 进入快速通道的委托
- 使用anyio替代asyncio
- Based on the flask to write a small shopping mall project
- 牛客网刷题
- Spark efficient data analysis 01. Establishment of idea development environment
- 『面试知识集锦100篇』1.面试技巧篇丨HR的小心思,你真的懂吗?
- Analysis of QT basic engineering
- "100 Interview Knowledge Collections" 1. Interview Skills丨Do you really understand HR's careful thinking?
猜你喜欢

Summer vacation training week1

QWidget、QDialog、QMainWindow 的异同点

DNS protocol, ICMP protocol, NAT technology

IPv6 Foundation

为什么应该在开发环境中使用 Kubernetes

【图像处理】基于中轴变换实现图像骨架提取附matlab代码

Starrocks technology insider: how to have both real-time update and fast query

如何在匹配行之前使用 grep 显示文件名和行号

Meeting OA project (V) -- meeting notice and feedback details

Peking University open classes are coming! Welcome to the "AI for science" class
随机推荐
On CompareTo method in string
One click blog building: how to use WordPress plug-in to build a dedicated blog
TCP和UDP
开源峰会抢先看 | 7 月 29 日分论坛 & 活动议程速览
Meituan and hungry were interviewed by Hangzhou supervisors to implement the responsibility of food safety management and prohibit malicious competition
Conference OA project - my approval
Basic. Blocking
Pyqt5 rapid development and practice 6.6 qformlayout & 6.7 nested layout & 6.8 qsplitter
Exclusive interview | Cheng Li, chief technology officer of Alibaba: cloud + open source together form a credible foundation for the digital world
Hutool日期时间
什么是 Kubernetes 自定义资源定义 (CRD)?
DNS协议、ICMP协议、NAT技术
Function comparison between report control FastReport and stimulus soft
Alluxio为Presto赋能跨云的自助服务能力
Similarities and differences of QWidget, qdialog and qmainwindow
大伟 Golang之路
Start from scratch blazor server (3) -- add cookie authorization
️ 炒 股 实 战丨原 地 起 飞 ️
[image processing] image skeleton extraction based on central axis transformation with matlab code
重磅 | 2022 开放原子全球开源峰会在北京开幕