异步后台任务。
asyncbg的Python项目详细描述
异步后台任务
Python3.7及更高版本中的异步后台任务。
在不阻塞异步的情况下运行CPU密集型的长时间运行任务 循环,在 多处理模块。
安装
pipinstallasyncbg
示例
在examples folder中有更多的示例。
呼叫
在另一个进程中调用work(a, b)。脚本输出是Result: 9。
importasyncioimportasyncbgdefwork(a,b):returna+basyncdefmain():result=awaitasyncbg.call(work,4,5)print(f'Result: {result}')asyncio.run(main())
工艺池
创建一个包含两个worker的进程池,并调用work()3 次数(最多并行调用两次回调)。
importasyncioimportasyncbgdefwork():passasyncdefmain():pool=asyncbg.ProcessPoolExecutor(max_workers=2)awaitasyncio.gather(pool.call(work),pool.call(work),pool.call(work))asyncio.run(main())