python异步编程,协程

python异步编程,协程

Python的协程通过在单线程事件循环中碰到await关键字切换执行以最大化的利用资源,避免阻塞等待,且避免了线程和进程上下文切换带来的资源消耗,且无需考虑临界互斥问题。

针对python异步编程中几个常用的api做了总结梳理。

python 复制代码
import asyncio
import random
import traceback
from asyncio import Task
from typing import TypeVar, Union, Tuple, Set

T = TypeVar('T')


async def exception_task(val):
    await asyncio.sleep(random.randint(1, 10))
    if val % 2:
        raise ValueError(val)
    return val


async def as_completed():
    coroutines = [asyncio.create_task(exception_task(_)) for _ in range(10)]
    # as_completed返回一个生成器,一旦有协程执行完毕即返回,不保证顺序
    for coroutine in asyncio.as_completed(coroutines, timeout=20):
        try:
            result = await coroutine
            print(result)
        except BaseException:
            traceback.print_exc()


async def gather():
    # 如果列表中的协程没有await, 不会报警告
    coroutines = [asyncio.create_task(exception_task(_)) for _ in range(10)]

    # 如果列表中的协程没有await, 报警告, 除非手动close
    coroutines = [exception_task(i) for i in coroutines]
    # 手动close
    # for i in coroutines:
    #     i.close()

    # 按顺序返回协程运行的结果
    # return_exception为False, 协程中的异常直接抛出到此行代码,如果想要程序不崩溃,需要try该行代码
    results: Tuple[T, ...] = await asyncio.gather(*coroutines, return_exceptions=False)

    # 返回结果可能是异常对象和协程执行结果
    results: Tuple[Union[T, BaseException], ...] = await asyncio.gather(*coroutines, return_exceptions=True)
    for result in results:
        if isinstance(result, BaseException):
            print(result)
        else:
            print(result)


async def wait_return_fisrt():
    coroutines = [asyncio.create_task(exception_task(_)) for _ in range(10)]

    # 第一个协程执行完开始返回,返回结果为Future
    while True:
        status = await asyncio.wait(coroutines, return_when=asyncio.FIRST_COMPLETED)
        done: Set[Task[T]] = status[0]
        pending: Set[Task[T]] = status[1]
        print(len(pending), len(done))
        if len(pending) == 0:
            break

    for x in done:
        # 如果发生异常,必须调用此函数,否则未处理异常会被抛出
        if x.exception():
            print(f'Exception: {x.exception()}')
        else:
            print(x.result())


async def wait_first_exception():
    coroutines = [asyncio.create_task(exception_task(_)) for _ in range(10)]
    while True:
        done, pending = await asyncio.wait(coroutines, return_when=asyncio.FIRST_EXCEPTION)
        print(len(done), len(pending))
        if len(pending) == 0:
            break
    for x in done:
        if x.exception():
            print(f'Exception: {x.exception()}')
        else:
            print(x.result())


async def wait_all_completed():
    coroutines = [asyncio.create_task(exception_task(_)) for _ in range(10)]
    done, _ = await asyncio.wait(coroutines, return_when=asyncio.ALL_COMPLETED)
    for x in done:
        if x.exception():
            print(f'Exception: {x.exception()}')
        else:
            print(x.result())


if __name__ == '__main__':
    try:
        asyncio.run(wait_first_exception())
    except KeyboardInterrupt:
        print('Interrupted')
    except Exception as e:
        print('Exception')
        raise e
  1. 无法在协程中捕获KeyboardInterrupt异常,只能在协程外捕获,捕获之后可以做一些清理工作,比如将没有await的协程close。
  2. gather方法的封装层次较高,用于仅关心协程执行结果的情况,返回结果是按原顺序的
    • return_exceptions为False,任一协程抛出异常后无法拿到任何协程的执行结果,且需要在协程外捕获异常,否则程序崩溃。
    • return_exceptions为True,返回结果为Tuple[Union[T, BaseException]],需要判断返回结果的类型。
  3. as_completed,返回一个生成器,该生成器尽快地返回已经执行完的Future对象,没有顺序,可设置超时时间。
  4. wait,返回结果为Tuple[Set[Task[T]],Set[Task[T]]],可被多次调用,不断更新已完成的协程集合和未完成的协程集合,不保证顺序,可设置超时时间。
    • return_when=ALL_COMPLETED,所有协程执行完毕后返回,此时未完成的协程集合一定为空,需要使用result方法或者exception方法获取协程的执行结果。
    • return_when=FIRST_COMPLETED,当有协程执行完毕后返回,重复调用wait方法不断更新两个集合,需要使用result方法或者exception方法获取协程的执行结果。
    • return_when=FIRST_EXCEPTION,当有协程抛出异常后返回,否则直到所有协程执行完毕后返回,重复调用wait方法不断更新两个集合,需要使用result方法或者exception方法获取协程的执行结果。
相关推荐
测试员周周3 小时前
【Appium 系列】第16节-WebView-H5上下文切换 — 混合应用的自动化难点
运维·开发语言·人工智能·功能测试·appium·自动化·测试用例
测试19983 小时前
软件测试 - 单元测试总结
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
Mahir085 小时前
Spring 循环依赖深度解密:从问题本质到三级缓存源码级解析
java·后端·spring·缓存·面试·循环依赖·三级缓存
曲幽5 小时前
我用了FastApiAdmin后,连夜把踩过的坑都整理出来了
redis·python·postgresql·vue3·fastapi·web·sqlalchemy·admin·fastapiadmin
杜子不疼.5 小时前
【C++ AI 大模型接入 SDK】 - DeepSeek 模型接入(上)
开发语言·c++·chatgpt
加号36 小时前
【C#】 串口通信技术深度解析及实现
开发语言·c#
sycmancia6 小时前
Qt——编辑交互功能的实现
开发语言·qt
RyFit6 小时前
SpringAI 常见问题及解决方案大全
java·ai
石山代码7 小时前
C++ 内存分区 堆区
java·开发语言·c++
前端若水7 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js