1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
.. aiojobs documentation master file, created by
sphinx-quickstart on Sat Jul 1 15:24:45 2017.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
aiojobs: Jobs scheduler for managing background task
====================================================
The library gives controlled way for scheduling background tasks for
:mod:`asyncio` applications.
Installation
------------
.. code-block:: bash
$ pip3 install aiojobs
Usage example
-------------
.. code-block:: python
import asyncio
import aiojobs
async def coro(timeout):
await asyncio.sleep(timeout)
async def main():
async with aiojobs.Scheduler() as 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
# Exit from context will gracefully wait on tasks before closing
# any remaining spawned jobs
asyncio.run(main())
For further information read :ref:`aiojobs-quickstart`,
:ref:`aiojobs-intro` and :ref:`aiojobs-api`.
Shielding tasks with a scheduler
--------------------------------
It is typically recommended to use :func:`asyncio.shield` to protect tasks
from cancellation. However, the inner shielded tasks can't be tracked and
are therefore at risk of being cancelled during application shutdown.
To resolve this issue aiojobs includes a :meth:`aiojobs.Scheduler.shield`
method to shield tasks while also keeping track of them in the scheduler.
In combination with the :meth:`aiojobs.Scheduler.wait_and_close` method,
this allows shielded tasks the required time to complete successfully
during application shutdown.
For example:
.. code-block:: python
import asyncio
import aiojobs
from contextlib import suppress
async def important():
print("START")
await asyncio.sleep(5)
print("DONE")
async def run_something(scheduler):
# If we use asyncio.shield() here, then the task doesn't complete and DONE is never printed.
await scheduler.shield(important())
async def main():
async with aiojobs.Scheduler() as scheduler:
t = asyncio.create_task(run_something(scheduler))
await asyncio.sleep(0.1)
t.cancel()
with suppress(asyncio.CancelledError):
await t
asyncio.run(main())
Integration with aiohttp.web
----------------------------
.. code-block:: python
from aiohttp import web
from aiojobs.aiohttp import setup, spawn
import aiojobs
async def handler(request):
await spawn(request, coro())
return web.Response()
app = web.Application()
app.router.add_get('/', handler)
setup(app)
or just
.. code-block:: python
from aiojobs.aiohttp import atomic
@atomic
async def handler(request):
return web.Response()
Source code
-----------
The project is hosted on GitHub: https://github.com/aio-libs/aiojobs
Please feel free to file an issue on the bug tracker if you have found
a bug or have some suggestion in order to improve the library.
Communication channels
----------------------
*Gitter Chat* https://gitter.im/aio-libs/Lobby
We support `Stack Overflow <https://stackoverflow.com>`_.
Please add *python-asyncio* or *aiohttp* tag to your question there.
Author and License
-------------------
The ``aiojobs`` package is written by Andrew Svetlov.
It's *Apache 2* licensed and freely available.
.. toctree::
:maxdepth: 2
:caption: Contents:
quickstart
intro
api
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
|