Skip to main content

重生之asyncioList:异步列表的救赎

前世的我,在异步编程的泥潭里反复挣扎——多协程操作列表时的race condition(竞态条件)像幽灵一样挥之不去,手动加锁时的繁琐代码让逻辑支离破碎,想在异步迭代里遍历列表却总被中途修改的元素搞得晕头转向……直到某次线上事故因为列表并发操作崩溃,我才痛悟:异步环境下的列表操作,真的需要一个简单却靠谱的解决方案。

重生归来,带着对异步编程痛点的深刻理解,我写下了asyncioList——一个简单到几乎不用学,却能解决异步列表操作所有麻烦的工具。

它为什么值得你用?

asyncioList没什么惊天动地的黑科技,却把异步列表该有的样子做到了极致:

  • 简单得像在用普通列表:append、pop、get、index……所有方法都和原生列表一一对应,只是加了await关键字,上手零成本。
  • 天生抗并发:内置asyncio.Lock,不管多少协程同时操作,都能保证数据安全,不用再自己手动加锁解锁。
  • 异步场景全适配:支持异步迭代(async for)、等待列表变化(wait_for_change)、上下文管理器批量操作,完美融入你的异步代码流。
  • 轻得像一阵风:只依赖Python标准库,安装即用,不会给你的项目增加任何额外负担。

快速上手:重生后的第一次操作

安装

# 从源码安装
pip install git+https://github.com/303-SeeOther/asyncioList.git
# 或者从pypi安装
pip install asyncioList

基础用法(1分钟就能学会你已经会了)

import asyncio
from asyncioList.AsyncioList import AsyncioList

async def main():
    # 创建列表(和普通列表一样简单)
    my_list = AsyncioList([1, 2, 3])
    
    # 加个元素(加个await就好)
    await my_list.append(4)
    print(await my_list.get(3))  # 输出:4
    
    # 删个元素
    await my_list.pop(0)
    print(await my_list.length())  # 输出:3
    
    # 查个索引
    print(await my_list.index(3))  # 输出:1

asyncio.run(main())

进阶玩法:那些前世没享过的便利

异步迭代,遍历不慌

async def iterate_example():
    fruits = AsyncioList(['apple', 'banana', 'cherry'])
    async for fruit in fruits:  # 直接用async for遍历
        print(fruit)

等待列表变化,实时响应

async def watch_example():
    my_list = AsyncioList()
    
    # 一个协程修改列表
    async def add_items():
        await asyncio.sleep(1)
        await my_list.append("重生了!")
    
    # 另一个协程等待变化
    async def watcher():
        await my_list.wait_for_change()  # 等列表变了再继续
        print(await my_list.get(0))  # 输出:重生了!
    
    await asyncio.gather(add_items(), watcher())

上下文管理器,批量操作更高效

async def context_example():
    async with AsyncioList([1, 2, 3]) as ctx_list:
        # 批量操作直接改内部列表,省掉多次加锁开销
        ctx_list._items.extend([4, 5, 6])
        ctx_list._items.reverse()
    print(await ctx_list.slice(0, 6))  # 输出:[6, 5, 4, 3, 2, 1]

结尾(凑字数用的)

asyncioList真的很简单,只是个小工具,但是在异步编程中操作列表时,总会遇到一些问题,比如race condition(竞态条件)、死锁等。这些问题都可以通过asyncioList来避免,避免了手动加锁解锁的繁琐代码,同时也避免了race condition的问题。 发到pypi上只是方便自己拉,是为了帮助大家节省一点时间,少掉几根头发。 如果你的异步代码里也需要操作列表,欢迎使用asyncioList。

↑↑以上内容均由豆包生成

Metadata

Release files for asyncioList 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asyncioList 1.0
File Size Uploaded Provenance
asynciolist-1.0.tar.gz 6.6 kB Without provenance Details

Release files / asynciolist-1.0.tar.gz

Download URL asynciolist-1.0.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2b1ef8c0b115a0c166e6693ca6ee27a521d43e8f0136606f8083fdf75896061b
BLAKE2b-256 checksum
How to use checksums
a503758cb3921e6b591bb4f4b95a2325e4e625d4e01eca99c632fc699d5dcaab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Security for asyncioList 1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page