Skip to content

Commit 7d21f71

Browse files
committed
[7.9] Add ASGI to async docs
1 parent 4d24076 commit 7d21f71

File tree

1 file changed

+66
-54
lines changed

1 file changed

+66
-54
lines changed

docs/async.rst

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,19 @@ Using Asyncio with Elasticsearch
44
.. py:module:: elasticsearch
55
66
Starting in ``elasticsearch-py`` v7.8.0 for Python 3.6+ the ``elasticsearch`` package supports async/await with
7-
`Asyncio <https://docs.python.org/3/library/asyncio.html>`_. Install the package with the ``async``
8-
extra to install the `aiohttp <https://docs.aiohttp.org>`_ HTTP client and other dependencies
9-
required for async support:
7+
`Asyncio <https://docs.python.org/3/library/asyncio.html>`_ and `Aiohttp <https://docs.aiohttp.org>`_.
8+
You can either install ``aiohttp`` directly or use the ``[async]`` extra:
109

1110
.. code-block:: bash
1211
13-
$ python -m pip install elasticsearch[async]>=7.8.0
14-
15-
The same version specifiers for following the Elastic Stack apply to
16-
the ``async`` extra:
12+
$ python -m pip install elasticsearch>=7.8.0 aiohttp
1713
18-
.. code-block:: bash
14+
# - OR -
1915
20-
# Elasticsearch 7.x
21-
$ python -m pip install elasticsearch[async]>=7,<8
16+
$ python -m pip install elasticsearch[async]>=7.8.0
2217
2318
.. note::
24-
Async functionality is a new feature of this library in v7.8.0 so
19+
Async functionality is a new feature of this library in v7.8.0+ so
2520
`please open an issue <https://github.com/elastic/elasticsearch-py/issues>`_
2621
if you find an issue or have a question about async support.
2722

@@ -51,6 +46,66 @@ and are used in the same way as other APIs, just with an extra ``await``:
5146
5247
All APIs that are available under the sync client are also available under the async client.
5348

49+
ASGI Applications and Elastic APM
50+
---------------------------------
51+
52+
`ASGI <https://asgi.readthedocs.io>`_ (Asynchronous Server Gateway Interface) is a new way to
53+
serve Python web applications making use of async I/O to achieve better performance.
54+
Some examples of ASGI frameworks include FastAPI, Django 3.0+, and Starlette.
55+
If you're using one of these frameworks along with Elasticsearch then you
56+
should be using :py:class:`~elasticsearch.AsyncElasticsearch` to avoid blocking
57+
the event loop with synchronous network calls for optimal performance.
58+
59+
`Elastic APM <https://www.elastic.co/guide/en/apm/agent/python/current/index.html>`_
60+
also supports tracing of async Elasticsearch queries just the same as
61+
synchronous queries. For an example on how to configure ``AsyncElasticsearch`` with
62+
a popular ASGI framework `FastAPI <https://fastapi.tiangolo.com/>`_ and APM tracing
63+
there is a `pre-built example <https://github.com/elastic/elasticsearch-py/tree/master/examples/fastapi-apm>`_
64+
in the ``examples/fastapi-apm`` directory.
65+
66+
Frequently Asked Questions
67+
--------------------------
68+
69+
NameError / ImportError when importing ``AsyncElasticsearch``?
70+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71+
72+
If when trying to use ``AsyncElasticsearch`` and you're receiving a ``NameError`` or ``ImportError``
73+
you should ensure that you're running Python 3.6+ (check with ``$ python --version``) and
74+
that you have ``aiohttp`` installed in your environment (check with ``$ python -m pip freeze | grep aiohttp``).
75+
If either of the above conditions is not met then async support won't be available.
76+
77+
What about the ``elasticsearch-async`` package?
78+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79+
80+
Previously asyncio was supported separately via the `elasticsearch-async <https://github.com/elastic/elasticsearch-py-async>`_
81+
package. The ``elasticsearch-async`` package has been deprecated in favor of
82+
``AsyncElasticsearch`` provided by the ``elasticsearch`` package
83+
in v7.8 and onwards.
84+
85+
Receiving 'Unclosed client session / connector' warning?
86+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87+
88+
This warning is created by ``aiohttp`` when an open HTTP connection is
89+
garbage collected. You'll typically run into this when closing your application.
90+
To resolve the issue ensure that :meth:`~elasticsearch.AsyncElasticsearch.close`
91+
is called before the :py:class:`~elasticsearch.AsyncElasticsearch` instance is garbage collected.
92+
93+
For example if using FastAPI that might look like this:
94+
95+
.. code-block:: python
96+
97+
from fastapi import FastAPI
98+
from elasticsearch import AsyncElasticsearch
99+
100+
app = FastAPI()
101+
es = AsyncElasticsearch()
102+
103+
# This gets called once the app is shutting down.
104+
@app.on_event("shutdown")
105+
async def app_shutdown():
106+
await es.close()
107+
108+
54109
Async Helpers
55110
-------------
56111

@@ -147,49 +202,6 @@ Reindex
147202
.. autofunction:: async_reindex
148203

149204

150-
Frequently Asked Questions
151-
--------------------------
152-
153-
NameError / ImportError when importing ``AsyncElasticsearch``?
154-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
155-
156-
If when trying to use ``AsyncElasticsearch`` and you're receiving a ``NameError`` or ``ImportError``
157-
you should ensure that you're installing via the ``elasticsearch[async]`` extra given above.
158-
The ``AsyncElasticsearch`` name won't be available unless ``aiohttp`` and other async dependencies
159-
are installed and you're using Python 3.6 or later.
160-
161-
What about the ``elasticsearch-async`` package?
162-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
163-
164-
Previously asyncio was supported separately via the `elasticsearch-async <https://github.com/elastic/elasticsearch-py-async>`_ package.
165-
elasticsearch-async has been deprecated in favor of ``elasticsearch`` async support.
166-
For Elasticsearch 7.x and later you must install
167-
``elasticsearch[async]`` and use ``elasticsearch.AsyncElasticsearch()``.
168-
169-
Receiving 'Unclosed client session / connector' warning?
170-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171-
172-
This warning is created by ``aiohttp`` when an open HTTP connection is
173-
garbage collected. You'll typically run into this when closing your application.
174-
To resolve the issue ensure that :meth:`~elasticsearch.AsyncElasticsearch.close`
175-
is called before the :py:class:`~elasticsearch.AsyncElasticsearch` instance is garbage collected.
176-
177-
For example if using FastAPI that might look like this:
178-
179-
.. code-block:: python
180-
181-
from fastapi import FastAPI
182-
from elasticsearch import AsyncElasticsearch
183-
184-
app = FastAPI()
185-
es = AsyncElasticsearch()
186-
187-
# This gets called once the app is shutting down.
188-
@app.on_event("shutdown")
189-
async def app_shutdown():
190-
await es.close()
191-
192-
193205
API Reference
194206
-------------
195207

0 commit comments

Comments
 (0)