Skip to content

Async support #1672

Closed
Closed
@sla-te

Description

@sla-te

Thank you for upgrading the lib to support v8.x.

Maybe this is a good time to ask for officially adding async support according to https://elasticsearch-py.readthedocs.io/en/v8.9.0/async.html, as it is clearly a feature, that will be used by a lot of users (and already has quite a history of requests about it).

I found #1435 which was closed in favor of #1480, which is apparently already 2 years old.

That all being said, we have been using the following "hack", to achieve async support in the past (using v7.4.1):

import asyncio

from elasticsearch import AsyncElasticsearch
from elasticsearch_dsl import Search

config = {...: ...}
indexname = ...
es = AsyncElasticsearch(**config)


async def do_es_async_work():
    s = Search(index=indexname).filter(...).filter(...)
    r = await s.execute().to_dict()
    return r

print(asyncio.run(do_es_async_work()))

On trying it with the newly released version (v.8.9.0), this now sadly throws:

RuntimeWarning: coroutine 'AsyncElasticsearch.search' was never awaited

Upon looking at diffs between v7.4.1 and v.8.9.0, I was able to identify the root of the problem at

self._response = self._response_class(

It seems, that wrapping the response of the call to es.search in an object, now rendered this hack to fail, as body would only exist after awaiting es.search, if AsyncElasticsearch is used as connection. - Now the problem coming up with the new structure is, that I do not see a way to create a new "hack" to achieve passing through the async call through to AsyncElasticsearch directly, without starting to use e.g. asyncio.run_in_executor, which would cause a terrible overhead, as every async call would open a new thread, which kind of destroys the advantage of using async in this scenario if I understand this correctly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions