当前位置:网站首页>使用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)
边栏推荐
- Kunlunbase instruction manual (I) quick installation manual
- Hugo NexT V4 介绍
- Meituan and hungry were interviewed by Hangzhou supervisors to implement the responsibility of food safety management and prohibit malicious competition
- Roots of equations in R language dichotomy and Newton iteration
- PHP basics uses arrays to save data
- LeetCode_ 1049_ Weight of the last stone II
- PaddleLite 编译以及代码跑通复盘
- Spark efficient data analysis 01. Establishment of idea development environment
- WPF 截图控件之绘制方框与椭圆(四) 「仿微信」
- 判断两个对象的值是否都相等
猜你喜欢

Kunlunbase instruction manual (I) quick installation manual

Kunlunbase instruction manual (II) best practices for peer-to-peer deployment

Kunlunbase instruction manual (III) data import & synchronization

Detailed arrangement of JVM knowledge points (long text warning)

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

QWidget、QDialog、QMainWindow 的异同点

The 2022 open atom global open source summit opened in Beijing

TCP和UDP

阿里P8爆出的这份大厂面试指南,看完工资暴涨30k!

Starrocks technology insider: how to have both real-time update and fast query
随机推荐
Alibaba P8 broke out this interview guide for big factories. After reading it, the salary soared by 30K!
如何开始为您的 Kubernetes 应用程序编写 Helm 图表
Hutool日期时间
Niuke net brush questions
sql join中on条件后接and和where
阿里架构师耗时一年整理的《Lucene高级文档》,吃透你也是大厂员工!
How can agile development reduce cognitive bias in collaboration| Agile way
The heavyweight foundation awarded platinum, gold and silver donors
StarRocks 技术内幕:实时更新与极速查询如何兼得
开源峰会抢先看 | 7月29日分论坛&活动议程速览
2022最新 wifi大师小程序独立版3.0.8
Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
聊聊性能测试环境搭建
如何使用“COPY –link”加速 Docker 构建和优化缓存
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
Kunlunbase instruction manual (I) quick installation manual
『知识集锦』一文搞懂mysql索引!!(建议收藏)
Luogu p1816 loyalty solution
DOD and Dor, two artifacts to reduce "cognitive bias"
JVM知识点详细整理(长文警告)