当前位置:网站首页>使用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)
边栏推荐
- 小笑授权系统V5.0开心版
- 阿里架构师耗时一年整理的《Lucene高级文档》,吃透你也是大厂员工!
- ECCV 2022 | ssp: a new idea of small sample tasks with self-supporting matching
- 如何使用 grep 跨多行查找模式匹配
- 大伟 GBase8s游标稳定性读ESQL测试用例
- DOD and Dor, two artifacts to reduce "cognitive bias"
- Factoextra: visualization of multivariate statistics
- Kunlun storage vs PostgreSQL OLTP test
- 【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
- Roots of equations in R language dichotomy and Newton iteration
猜你喜欢
Drawing box and ellipse of WPF screenshot control (IV) "imitating wechat"
Function comparison between report control FastReport and stimulus soft
Leetcode bit operation
WPF 截图控件之绘制方框与椭圆(四) 「仿微信」
VMware: use commands to update or upgrade VMware esxi hosts
IPv6 Foundation
TCP和UDP
Leetcode binary tree series -- 144. Preorder traversal of binary trees
PHP basics uses arrays to save data
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
随机推荐
Exclusive interview | Cheng Li, chief technology officer of Alibaba: cloud + open source together form a credible foundation for the digital world
How to realize the function of adding watermark
Spark高效数据分析02、基础知识13篇
Using Riemann sum to calculate approximate integral in R language
8. Interleave - understand ThreadPoolExecutor thread pool from architecture design to practice
「PHP基础知识」使用数组保存数据
ECCV 2022 | ssp: a new idea of small sample tasks with self-supporting matching
Spark efficient data analysis 01. Establishment of idea development environment
R package pedquant realizes stock download and financial quantitative analysis
Determine whether the values of two objects are equal
2022最新 wifi大师小程序独立版3.0.8
Meeting OA project (V) -- meeting notice and feedback details
QT工程基本构建
Starrocks technology insider: how to have both real-time update and fast query
多线程顺序运行的 4 种方法,面试随便问!
StarRocks 技术内幕:实时更新与极速查询如何兼得
Conference OA project - my approval
Package delivery (greedy)
如何开始为您的 Kubernetes 应用程序编写 Helm 图表
leetcode-位运算