当前位置:网站首页>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)
边栏推荐
- [image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
- Hutool日期时间
- 北京大学公开课重磅来袭!欢迎走进「AI for Science」课堂
- 专访 | 阿里巴巴首席技术官程立:云 + 开源共同形成数字世界的可信基础
- 大伟 GBase8s游标稳定性读ESQL测试用例
- Package Delivery(贪心)
- 带你浅聊一下PHP搭建的电商商城系统
- AMH6.X升级到AMH7.0后,登录后台提示MySQL连接出错怎么解决?
- Golang realizes file upload and download
- 「PHP基础知识」使用数组保存数据
猜你喜欢

The heavyweight foundation awarded platinum, gold and silver donors
![[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code](/img/c1/f962f1c1d9f75732157d49a5d1d0d6.png)
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code

Pytorch 入门

一键搭建博客:如何使用WordPress插件搭建专属博客

ECCV 2022 | ssp: a new idea of small sample tasks with self-supporting matching

2022 latest WiFi master applet independent version 3.0.8

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

Discussion on the application of arcing smart electricity in elderly care institutions

QT工程基本构建

牛客网刷题
随机推荐
如何使用“COPY –link”加速 Docker 构建和优化缓存
Function comparison between report control FastReport and stimulus soft
[unity, C #] character keyboard input steering and rotation
SkiaSharp of WPF custom painting to bounce ball (case)
2022最新 wifi大师小程序独立版3.0.8
HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态
QML(一):自定义圆角按钮的处理
专访 | 阿里巴巴首席技术官程立:云 + 开源共同形成数字世界的可信基础
One click blog building: how to use WordPress plug-in to build a dedicated blog
Basic construction of QT project
Std:: vector copy, append, nested access
matplotlib中文问题
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
报表控件FastReport与StimulSoft功能对比
Regular expression matching URL
PHP basics uses arrays to save data
Is this it?TypeScript actually not difficult!(recommended collection)
DOD and Dor, two artifacts to reduce "cognitive bias"
重磅 | 2022 开放原子全球开源峰会在北京开幕
Summer vacation training week1