作者:Each+ Apollo
1. 理解
aiojobs可以构建方便管理的异步任务
2. 安装
pip install aiojobs
2. 使用
create_scheduler(close_time=0.1, limit=100, pending_limit=10000, exception_handler=None)
- exception_handler: 如果出现异常,会调用exception_handler传入的方法,如果没有传入任何方法,会抛出异常报错
- limit:同时执行的任务数上限
import asyncio
import aiojobs
async def func():
for i in range(100):
await asyncio.sleep(1)
print(i)
if i == 4:
raise Exception('warning')
def exception_handler(job, errinfo):
print(job)
print(errinfo)
async def main():
jobs = await aiojobs.create_scheduler(
exception_handler=exception_handler,
)
await jobs.spawn(func())
await asyncio.sleep(6)
await jobs.close()
asyncio.get_event_loop().run_until_complete(main())
执行结果如下
0
1
2
3
4
<Scheduler jobs=0>
{'message': 'Job processing failed', 'job': <Job coro=<<coroutine object func at 0x7f841a669e60>>>, 'exception': Exception('warning',)}