-
Notifications
You must be signed in to change notification settings - Fork 1.2k
add readthedocs content to the guide #2769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
[[async]] | ||
== Using with asyncio | ||
|
||
The `elasticsearch` package supports async/await with | ||
https://docs.python.org/3/library/asyncio.html[asyncio] and | ||
https://docs.aiohttp.org[aiohttp]. You can either install `aiohttp` | ||
directly or use the `[async]` extra: | ||
|
||
[source,bash] | ||
---- | ||
$ python -m pip install elasticsearch aiohttp | ||
|
||
# - OR - | ||
|
||
$ python -m pip install elasticsearch[async] | ||
---- | ||
|
||
[discrete] | ||
=== Getting Started with Async | ||
|
||
After installation all async API endpoints are available via | ||
`~elasticsearch.AsyncElasticsearch` and are used in the same way as | ||
other APIs, with an extra `await`: | ||
|
||
[source,python] | ||
---- | ||
import asyncio | ||
from elasticsearch import AsyncElasticsearch | ||
|
||
client = AsyncElasticsearch() | ||
|
||
async def main(): | ||
resp = await client.search( | ||
index="documents", | ||
body={"query": {"match_all": {}}}, | ||
size=20, | ||
) | ||
print(resp) | ||
|
||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(main()) | ||
---- | ||
|
||
All APIs that are available under the sync client are also available | ||
under the async client. | ||
|
||
https://elasticsearch-py.readthedocs.io/en/latest/async.html#api-reference[Reference documentation] | ||
|
||
[discrete] | ||
=== ASGI Applications and Elastic APM | ||
|
||
https://asgi.readthedocs.io[ASGI] (Asynchronous Server Gateway | ||
Interface) is a way to serve Python web applications making use of | ||
async I/O to achieve better performance. Some examples of ASGI | ||
frameworks include FastAPI, Django 3.0+, and Starlette. If you're | ||
using one of these frameworks along with Elasticsearch then you should | ||
be using `~elasticsearch.AsyncElasticsearch` to avoid blocking the event | ||
loop with synchronous network calls for optimal performance. | ||
|
||
https://www.elastic.co/guide/en/apm/agent/python/current/index.html[Elastic | ||
APM] also supports tracing of async Elasticsearch queries just the same | ||
as synchronous queries. For an example on how to configure | ||
`AsyncElasticsearch` with a popular ASGI framework | ||
https://fastapi.tiangolo.com/[FastAPI] and APM tracing there is a | ||
https://github.com/elastic/elasticsearch-py/tree/master/examples/fastapi-apm[pre-built | ||
example] in the `examples/fastapi-apm` directory. | ||
miguelgrinberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
See also the <<opentelemetry>> page. | ||
|
||
[discrete] | ||
=== Frequently Asked Questions | ||
|
||
[discrete] | ||
==== ValueError when initializing `AsyncElasticsearch`? | ||
|
||
If when trying to use `AsyncElasticsearch` you receive | ||
`ValueError: You must have 'aiohttp' installed to use AiohttpHttpNode` | ||
you should ensure that you have `aiohttp` installed in your environment | ||
(check with `$ python -m pip freeze | grep aiohttp`). Otherwise, | ||
async support won't be available. | ||
|
||
[discrete] | ||
==== What about the `elasticsearch-async` package? | ||
|
||
Previously asyncio was supported separately via the | ||
https://github.com/elastic/elasticsearch-py-async[elasticsearch-async] | ||
package. The `elasticsearch-async` package has been deprecated in favor | ||
of `AsyncElasticsearch` provided by the `elasticsearch` package in v7.8 | ||
and onwards. | ||
|
||
[discrete] | ||
==== Receiving 'Unclosed client session / connector' warning? | ||
|
||
This warning is created by `aiohttp` when an open HTTP connection is | ||
garbage collected. You'll typically run into this when closing your | ||
application. To resolve the issue ensure that | ||
`~elasticsearch.AsyncElasticsearch.close` is called before the | ||
`~elasticsearch.AsyncElasticsearch` instance is garbage collected. | ||
|
||
For example if using FastAPI that might look like this: | ||
|
||
[source,python] | ||
---- | ||
import os | ||
from contextlib import asynccontextmanager | ||
|
||
from fastapi import FastAPI | ||
from elasticsearch import AsyncElasticsearch | ||
|
||
ELASTICSEARCH_URL = os.environ["ELASTICSEARCH_URL"] | ||
client = None | ||
|
||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
global client | ||
client = AsyncElasticsearch(ELASTICSEARCH_URL) | ||
yield | ||
await client.close() | ||
|
||
app = FastAPI(lifespan=lifespan) | ||
|
||
@app.get("/") | ||
async def main(): | ||
return await client.info() | ||
---- | ||
|
||
You can run this example by saving it to `main.py` and executing | ||
`ELASTICSEARCH_URL=http://localhost:9200 uvicorn main:app`. | ||
|
||
[discrete] | ||
=== Async Helpers | ||
|
||
Async variants of all helpers are available in `elasticsearch.helpers` | ||
and are all prefixed with `async_*`. You'll notice that these APIs | ||
are identical to the ones in the sync <<client-helpers>> documentation. | ||
|
||
All async helpers that accept an iterator or generator also accept async | ||
iterators and async generators. | ||
miguelgrinberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
https://elasticsearch-py.readthedocs.io/en/latest/async.html#async-helpers[Reference documentation] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.