File: background_tasks.rst

package info (click to toggle)
quart 0.20.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,888 kB
  • sloc: python: 8,644; makefile: 42; sh: 17; sql: 6
file content (80 lines) | stat: -rw-r--r-- 2,622 bytes parent folder | download | duplicates (2)
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
.. _background_tasks:

Background tasks
================

If you have a task to perform where the outcome or result isn't
required you can utilise a background task to run it. Background tasks
run concurrently with the route handlers etc, i.e. in the
background. Background tasks are very useful when they contain actions
that take a lot of time to complete, as they allow a response to be
sent to the client whilst the task itself is carried out. Equally some
tasks just don't need to be completed before the response is sent and
instead can be done in the background.

Background tasks in Quart are created via the ``add_background_task``
method:

.. code-block:: python

    async def background_task():
        ...

    @app.route('/jobs/', methods=['POST'])
    async def create_job():
        app.add_background_task(background_task)
        return 'Success'

    @app.before_serving
    async def startup():
        app.add_background_task(background_task)


The background tasks will have access to the app context. The tasks
will be awaited during shutdown to ensure they complete before the app
shuts down. If your task does not complete within the config
``BACKGROUND_TASK_SHUTDOWN_TIMEOUT`` it will be cancelled.

Note ``BACKGROUND_TASK_SHUTDOWN_TIMEOUT`` should ideally be less than
any server shutdown timeout.

Synchronous background tasks are supported and will run in a separate
thread.

.. warning::

    As Quart is based on asyncio it will run on a single execution and
    switch between tasks as they become blocked on waiting on IO, if a
    task does not need to wait on IO it will instead block the event
    loop and Quart could become unresponsive. Additionally the task
    will consume the same CPU resources as the server and hence could
    slow the server.


Testing background tasks
------------------------

To ensure that background tasks complete in tests utilise the
``test_app`` context manager. This will wait for any background
tasks to complete before allowing the test to continue:

.. code-block:: python

    async def test_tasks_complete():
        async with app.test_app():
            app.add_background_task(...)
        # Background task has completed here
        assert task_has_done_something

Note when testing an app the ``test_client`` usage should be within
the ``test_app`` context block.

The background task coroutine function can be tested by creating an
app context and await the function,

.. code-block:: python

    async def test_background_task():
        async with app.app_context():
            await background_task()
        assert something_to_test