Jobs scheduler for managing background task (asyncio)
The library gives controlled way for scheduling background tasks for asyncio applications.
$ pip3 install aiojobs
import asyncio
import aiojobs
async def coro(timeout):
await asyncio.sleep(timeout)
async def main():
scheduler = await aiojobs.create_scheduler()
for i in range(100):
# spawn jobs
await scheduler.spawn(coro(i/10))
await asyncio.sleep(5.0)
# not all scheduled jobs are finished at the moment
# gracefully close spawned jobs
await scheduler.close()
asyncio.get_event_loop().run_until_complete(main())
from aiohttp import web
from aiojobs.aiohttp import setup, spawn
async def handler(request):
await spawn(request, coro())
return web.Response()
app = web.Application()
app.router.add_get('/', handler)
setup(app)
or just
from aiojobs.aiohttp import atomic
@atomic
async def handler(request):
return web.Response()
from sanic import Sanic, response, views
from aiojobs.sanic import setup, spawn
async def handler(request):
await spawn(request, coro())
return response.json({})
app = Sanic(__name__)
app.add_route(test, '/')
setup(app)
or just
from aiojobs.sanic import atomic
@atomic
async def handler(request):
return response.json({})
class Handler(views.HTTPMethodView):
decorators = [atomic]
async def get(self, request):
return response.json({})
For more information read documentation: https://aiojobs.readthedocs.io
aio-libs google group: https://groups.google.com/forum/#!forum/aio-libs
Feel free to post your questions and ideas here.
Gitter Chat https://gitter.im/aio-libs/Lobby
We support Stack Overflow. Please add python-asyncio or aiohttp tag to your question there.
The aiojobs
package is written by Andrew Svetlov.
It's Apache 2 licensed and freely available.