File: event_loop.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 (67 lines) | stat: -rw-r--r-- 2,017 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
.. _event_loop:

Customise the Event Loop
========================

Customising the event loop is often desired in order to use Quart with
another library whilst ensuring both use the same loop.  The best practice is
to create/initialise the third party within the loop created by Quart,
by using :ref:`startup_shutdown` ``before_serving`` functions as so,

.. code-block:: python

    @app.before_serving
    async def startup():
        loop = asyncio.get_event_loop()
        app.smtp_server = loop.create_server(aiosmtpd.smtp.SMTP, port=1025)
        loop.create_task(app.smtp_server)

    @app.after_serving
    async def shutdown():
        app.smtp_server.close()

Do not follow this pattern, typically seen in examples, because this creates a
new loop separate from the Quart loop for ThirdParty,

.. code-block:: python

    loop = asyncio.get_event_loop()
    third_party = ThirdParty(loop)
    app.run()  # A new loop is created by default

Controlling the event loop
--------------------------

It is the ASGI server running running Quart that owns the event loop
that Quart runs within, by default the server is Hypercorn. Both Quart
and Hypercorn allow the loop to be specified, the Quart shortcut in
development is to pass the loop to the ``app.run`` method,

.. code-block:: python

    loop = asyncio.get_event_loop()
    third_party = ThirdParty(loop)
    app.run(loop=loop)

or to use the ``app.run_task`` method,

.. code-block:: python

    loop = asyncio.get_event_loop()
    third_party = ThirdParty(loop)
    loop.run_until_complete(app.run_task())

the Hypercorn (production) solution is to utilise the `Hypercorn API
<https://hypercorn.readthedocs.io/en/latest/how_to_guides/api_usage.html>`_ to do the
following,

.. code-block:: python

    from hypercorn.asyncio import serve
    from hypercorn.config import Config
    ...
    loop = asyncio.get_event_loop()
    third_party = ThirdParty(loop)
    loop.run_until_complete(serve(app, Config()))
    # or even
    await serve(app, config)