Please forget you are using asyncio
Project description
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())
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
easy_async-0.1.0.tar.gz
(7.7 kB
view details)
Built Distribution
File details
Details for the file easy_async-0.1.0.tar.gz
.
File metadata
- Download URL: easy_async-0.1.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.3 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 234739ca5748beea88517256b3d68c5564d1988bd15a5595b977444e40830af4 |
|
MD5 | 1ec68627daeb3ee45c889ad7a9d122f6 |
|
BLAKE2b-256 | c28dcaf8d5f8e423474a884bb6c7770858a683b35215abe1a47375a2968d2703 |
File details
Details for the file easy_async-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: easy_async-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.3 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e5caceaee13da8ecd087c596d3f328020d7a36480b1bb132296f8f54640960a |
|
MD5 | 3d5b5ab8d3ff6047f5e7797b9963ee2b |
|
BLAKE2b-256 | 545c22d8632e443b7933665ba044855ce36206435af9555ba6b0eff388ce59e8 |