# easy_async
请你忘记 `asyncio` 的存在。
这是一个对 `asyncio` 进行高度封装的库,让你可以像写同步代码一样写异步代码,支持多线程,多进程。
## 功能
| 功能 | 支援 |
|--------|----|
| 多线程 | ✅ |
| 多进程 | ✅ |
| 简单易用 | ✅ |
| 全局退出管理 | ✅ |
## 安装
```bash
pip install easy-async
```
## 使用
```python
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())
```
Raw data
{
"_id": null,
"home_page": "https://github.com/majoson-chen/easy-async",
"name": "easy-async",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6,<4.0",
"maintainer_email": "",
"keywords": "asyncio,async,easy,easy-async",
"author": "Majoson",
"author_email": "majoson@qq.com",
"download_url": "https://files.pythonhosted.org/packages/c2/8d/caf8d5f8e423474a884bb6c7770858a683b35215abe1a47375a2968d2703/easy_async-0.1.0.tar.gz",
"platform": null,
"description": "# easy_async\n\n\u8bf7\u4f60\u5fd8\u8bb0 `asyncio` \u7684\u5b58\u5728\u3002\n\n\u8fd9\u662f\u4e00\u4e2a\u5bf9 `asyncio` \u8fdb\u884c\u9ad8\u5ea6\u5c01\u88c5\u7684\u5e93\uff0c\u8ba9\u4f60\u53ef\u4ee5\u50cf\u5199\u540c\u6b65\u4ee3\u7801\u4e00\u6837\u5199\u5f02\u6b65\u4ee3\u7801\uff0c\u652f\u6301\u591a\u7ebf\u7a0b\uff0c\u591a\u8fdb\u7a0b\u3002\n\n## \u529f\u80fd\n\n| \u529f\u80fd | \u652f\u63f4 |\n|--------|----|\n| \u591a\u7ebf\u7a0b | \u2705 |\n| \u591a\u8fdb\u7a0b | \u2705 |\n| \u7b80\u5355\u6613\u7528 | \u2705 |\n| \u5168\u5c40\u9000\u51fa\u7ba1\u7406 | \u2705 |\n\n## \u5b89\u88c5\n\n```bash\npip install easy-async\n```\n\n## \u4f7f\u7528\n\n```python\nimport asyncio\nimport multiprocessing\nimport threading\n\nfrom easy_async import EasyAsync, MISSION\n\n# scheduler will initial when every process start,\n# so different process will have different scheduler\n# Maximum 4k concurrent mission-task\nscheduler = EasyAsync(task_default_type=MISSION, complicating_limit=4096)\n\n\n# if you don't create scheduler before get_scheduler, it will create a scheduler with default params\n# scheduler = get_scheduler()\n\n# when you decorate a function with scheduler.as_mission,\n# it will be use `scheduler.create_task` when you call it\n@scheduler.as_mission\nasync def sleep(n):\n print(f'mission_task: sleep {n}')\n await asyncio.sleep(n)\n print(f'wake up: {n}')\n\n # \u5728\u6b64\u5904\u4e0d\u8981\u89e6\u53d1\u5faa\u73af\uff0cstop\u4e4b\u540e\u51fd\u6570\u9700\u8981\u8fd4\u56de\u3002\n\n if n == 5:\n print('sys stop')\n scheduler.stop()\n # scheduler.stop(force=True)\n\n\n# assign it is a daemon task, the scheduler will stop when all daemon-task finish.\n# when you call a non-args `scheduler.run` all registered daemon-task will be called.\n@scheduler.daemon()\nasync def assist():\n print('assist1 start')\n print('new_pid: ', multiprocessing.current_process().pid)\n print('new_tid: ', threading.current_thread().native_id)\n await sleep(1)\n\n print('assist1 end')\n\n\n@scheduler.daemon()\nasync def main():\n print('main start')\n print('mian scheduler', scheduler)\n\n # start_by_process\n scheduler.new(assist, method='process')\n\n # start mission-task manually\n await scheduler.add_mission(sleep(5))\n\n # start_by_thread\n # scheduler.new(assist, method='thread')\n\n # just use `scheduler.new` like `Process` or `Thread`\n\n await asyncio.sleep(3)\n print('main end')\n\n\nif __name__ == '__main__':\n # run all daemon_task\n # scheduler.run()\n\n # just run main()\n scheduler.run(main())\n\n```",
"bugtrack_url": null,
"license": "",
"summary": "Please forget you are using asyncio",
"version": "0.1.0",
"project_urls": {
"Homepage": "https://github.com/majoson-chen/easy-async",
"Repository": "https://github.com/majoson-chen/easy-async"
},
"split_keywords": [
"asyncio",
"async",
"easy",
"easy-async"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "545c22d8632e443b7933665ba044855ce36206435af9555ba6b0eff388ce59e8",
"md5": "3d5b5ab8d3ff6047f5e7797b9963ee2b",
"sha256": "6e5caceaee13da8ecd087c596d3f328020d7a36480b1bb132296f8f54640960a"
},
"downloads": -1,
"filename": "easy_async-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3d5b5ab8d3ff6047f5e7797b9963ee2b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6,<4.0",
"size": 8235,
"upload_time": "2023-08-25T18:58:02",
"upload_time_iso_8601": "2023-08-25T18:58:02.921487Z",
"url": "https://files.pythonhosted.org/packages/54/5c/22d8632e443b7933665ba044855ce36206435af9555ba6b0eff388ce59e8/easy_async-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c28dcaf8d5f8e423474a884bb6c7770858a683b35215abe1a47375a2968d2703",
"md5": "1ec68627daeb3ee45c889ad7a9d122f6",
"sha256": "234739ca5748beea88517256b3d68c5564d1988bd15a5595b977444e40830af4"
},
"downloads": -1,
"filename": "easy_async-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "1ec68627daeb3ee45c889ad7a9d122f6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6,<4.0",
"size": 7700,
"upload_time": "2023-08-25T18:58:04",
"upload_time_iso_8601": "2023-08-25T18:58:04.870940Z",
"url": "https://files.pythonhosted.org/packages/c2/8d/caf8d5f8e423474a884bb6c7770858a683b35215abe1a47375a2968d2703/easy_async-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-25 18:58:04",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "majoson-chen",
"github_project": "easy-async",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "easy-async"
}