Skip to main content

easy_async

请你忘记 asyncio 的存在。

这是一个对 asyncio 进行高度封装的库,让你可以像写同步代码一样写异步代码,支持多线程,多进程。

功能

功能 支援
多线程 ✅
多进程 ✅
简单易用 ✅
全局退出管理 ✅

安装

pip install easy-async

使用

import asyncio
import multiprocessing
import threading

from easy_async import EasyAsync, MISSION

# scheduler will initial when every process start,
# so different process will have different scheduler
#                                                Maximum 4k concurrent mission-task
scheduler = EasyAsync(task_default_type=MISSION, complicating_limit=4096)


# if you don't create scheduler before get_scheduler, it will create a scheduler with default params
# scheduler = get_scheduler()

# when you decorate a function with scheduler.as_mission,
# it will be use `scheduler.create_task` when you call it
@scheduler.as_mission
async def sleep(n):
    print(f'mission_task: sleep {n}')
    await asyncio.sleep(n)
    print(f'wake up: {n}')

    # 在此处不要触发循环,stop之后函数需要返回。

    if n == 5:
        print('sys stop')
        scheduler.stop()
        # scheduler.stop(force=True)


# assign it is a daemon task, the scheduler will stop when all daemon-task finish.
# when you call a non-args `scheduler.run` all registered daemon-task will be called.
@scheduler.daemon()
async def assist():
    print('assist1 start')
    print('new_pid: ', multiprocessing.current_process().pid)
    print('new_tid: ', threading.current_thread().native_id)
    await sleep(1)

    print('assist1 end')


@scheduler.daemon()
async def main():
    print('main start')
    print('mian scheduler', scheduler)

    # start_by_process
    scheduler.new(assist, method='process')

    # start mission-task manually
    await scheduler.add_mission(sleep(5))

    # start_by_thread
    # scheduler.new(assist, method='thread')

    # just use `scheduler.new` like `Process` or `Thread`

    await asyncio.sleep(3)
    print('main end')


if __name__ == '__main__':
    # run all daemon_task
    # scheduler.run()

    # just run main()
    scheduler.run(main())

Release files for easy-async 0.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 easy-async 0.1.0
File Size Uploaded
easy_async-0.1.0.tar.gz 7.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for easy-async 0.1.0
File Interpreter ABI Platform
easy_async-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.9 kB

Release files / easy_async-0.1.0.tar.gz

Download URL easy_async-0.1.0.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
234739ca5748beea88517256b3d68c5564d1988bd15a5595b977444e40830af4
BLAKE2b-256 checksum
How to use checksums
c28dcaf8d5f8e423474a884bb6c7770858a683b35215abe1a47375a2968d2703
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.3 Darwin/22.6.0

Release files / easy_async-0.1.0-py3-none-any.whl

Download URL easy_async-0.1.0-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6e5caceaee13da8ecd087c596d3f328020d7a36480b1bb132296f8f54640960a
BLAKE2b-256 checksum
How to use checksums
545c22d8632e443b7933665ba044855ce36206435af9555ba6b0eff388ce59e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.3 Darwin/22.6.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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