当前位置:网站首页>使用anyio替代asyncio
使用anyio替代asyncio
2022-07-29 10:58: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)
边栏推荐
- Ggdag draw DAG and cause and effect diagram
- Open source, compliance escort! 2022 open atom global open source summit open source compliance sub forum is about to open
- 什么是 Kubernetes 自定义资源定义 (CRD)?
- Regular expression matching URL
- 一键搭建博客:如何使用WordPress插件搭建专属博客
- Basic construction of QT project
- Discussion on the application of arcing smart electricity in elderly care institutions
- 就这?TypeScript其实并不难!(建议收藏)
- 3.认识和操作一下mysql的基本命令
- Start from scratch blazor server (3) -- add cookie authorization
猜你喜欢

Pyqt5 rapid development and practice 6.6 qformlayout & 6.7 nested layout & 6.8 qsplitter

如何使用 grep 跨多行查找模式匹配

Function comparison between report control FastReport and stimulus soft

LeetCode二叉树系列——144.二叉树的前序遍历

Kunlun storage vs PostgreSQL OLTP test

KRYSTAL:审计数据中基于知识图的战术攻击发现框架

DNS协议、ICMP协议、NAT技术

重磅 | 开放原子校源行活动正式启动

Alibaba P8 broke out this interview guide for big factories. After reading it, the salary soared by 30K!

【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
随机推荐
Hugo NexT V4 介绍
Regular expression matching URL
2022最新 wifi大师小程序独立版3.0.8
如何使用 grep 跨多行查找模式匹配
开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
Getting started with pytoch
QT基本工程的解析
深入理解C# 可空类型
Meeting OA project (V) -- meeting notice and feedback details
Hutool日期时间
ES6 arrow function this points to
Less than 10% of the 3 software test interview questions can be answered correctly! How many do you know?
判断两个对象的值是否都相等
Learning R language these ebooks are enough!
Luogu p1816 loyalty solution
Factoextra: visual PCA of multivariate statistical methods
Function comparison between report control FastReport and stimulus soft
3.认识和操作一下mysql的基本命令
Spark efficient data analysis 02, basic knowledge 13
Drawing box and ellipse of WPF screenshot control (IV) "imitating wechat"