Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Resolving `MissingGreenlet` error when using async drivers.

Abierto
#419 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Necesita aclaración
Estado de actividad
Estancado
Stack tecnológico
graphql, postgresql, python, sqlalchemy

Línea de trabajo

Comience con graphene_sqlalchemy/batching.py, especialmente con RelationshipLoader.batch_load_fn en las líneas enlazadas, y reproduzca el error MissingGreenlet usando la configuración descrita de aiohttp, asyncpg, PostgreSQL y GraphQL. Compare la ruta de SQLAlchemy 2 con la solución alternativa greenlet_spawn proporcionada; se considera terminado cuando haya una solución universal acordada o una documentación clara del comportamiento de los controladores asíncronos compatibles.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

I'm not sure if other people have run into this problem - I searched for days and couldn't find a clear answer. But eventually I figured out a solution, so wanted to document it here in case other people do run into it.

Background

Our setup uses a Postgres database which provides data for a GraphQL API, for which we initially used Flask, following the example. However, our schema uses a lot of 1:N relationships, so our performance was degraded due to the N+1 Round Trip Problem. The fix for this is, supposedly, very simple: just set batching=True in the meta class. However, doing this triggers the use of dataloaders, which operate asynchronously. So we switched from Flask to using the aiohttp web framework and asyncpg database driver, which are better suited for asynchronous tasks. This is when we first stumbled on the MissingGreenlet error: sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place?

Issue

The problem here comes from the RelationshipLoader.batch_load_fn() (link). As the function comment explains, that function has to "piggyback on some internal APIs" of sqlalchemy. As a result (I believe), the necessary greenlet_spawn function never gets called, and the above error is thrown. (Greenlets, AFAICT, are small thread-like objects that allow synchronous requests to be performed in an asynchronous environment. In this situation, the dataloader is what actually materializes the expected attributes, so it is necessary to make these calls synchronously)

Solution

So on to the fix: we just need to call greenlet_spawn ourselves. This can be done by simply redefining the batch_load_fn attribute on the RelationshipLoader class, so that all invocations call the corrected function rather than the incorrect code. My stripped down code is below (I removed some extraneous comments/checks/conditions from batch_load_fn for simplicitly, make sure that you compare to the original code and update the correct path for your situation):

from graphene_sqlalchemy.batching import RelationshipLoader
from sqlalchemy.orm import Session
from sqlalchemy.util import immutabledict
from sqlalchemy.util._concurrency_py3k import greenlet_spawn


def patch_relationship_loader() -> None:
    """
CALL THIS FUNCTION ONCE AS PART OF SETUP CODE
    """
    RelationshipLoader.batch_load_fn = batch_load_fn


async def batch_load_fn(self: RelationshipLoader, parents: Any) -> list[Any]:
    """
FOLLOWS THE `SQL_VERSION_HIGHER_EQUAL_THAN_2` PATH
    """
    child_mapper = self.relationship_prop.mapper
    parent_mapper = self.relationship_prop.parent
    session = Session.object_session(parents[0])

    for parent in parents:
        assert session is Session.object_session(parent)
        assert session and parent not in session.dirty

    states = [(sqlalchemy.inspect(parent), True) for parent in parents]

    query_context = None
    if session:
        parent_mapper_query = session.query(parent_mapper.entity)
        query_context = parent_mapper_query._compile_context()
# CALL greenlet_spawn HERE RATHER THAN CALLING _load_for_path DIRECTLY
        await greenlet_spawn(
            self.selectin_loader._load_for_path,
            query_context,
            parent_mapper._path_registry,
            states,
            None,
            child_mapper,
            None,
            None,  # recursion depth can be none
            immutabledict(),  # default value for selectinload->lazyload
        )
    result = [
        getattr(parent, self.relationship_prop.key) for parent in parents
    ]
    return result

Conclusion

I don't have enough knowledge of graphene-sqlalchemy or other database drivers to know what a universal solution would look like (or even if this is a universal problem - the dearth of information about this issue suggests not), so I don't want to submit a PR for this change, but at least in our situation this was the best fix. Hope it helps someone else out there!

Lenguaje dominante
Python
Estrellas
985
Forks
224
Métricas de merge de PR
Sin PR fusionados en 30 d

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de graphql-python/graphene-sqlalchemy

Todos los issues de graphql-python/graphene-sqlalchemy

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.