graphql-python / graphql-python/graphql-core-legacy

Some troubles using AsyncioExecutor and gunicorn

Ouverte
#283 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
Python
Étoiles
372
Forks
175
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

Hello!

My web app has many api (not only have graphql, but also some rest-api), I am using graphql-core 2.3.2 provided AsyncioExecutor as Executor, and use Gunicorn as WSGI server in production.

The problem is when broswer send some requests concurrently , AsyncioExecutor will raise a error: `RuntimeError: This event loop is already running`.

After my debugging, I found that it was because [AsyncioExecutor.wait_until_finished](https://github.com/graphql-python/graphql-core-legacy/blob/master/graphql/execution/executors/asyncio.py#L67) call `loop.run_until_complete`, then `run_until_complete` will check current loop if closed or running, and raise the error.

A worker of Gunicorn have some threads, those threads shares a event loop, and when a event loop is running, another concurrently request also call `run_until_complete` will raise RuntimeError, so they can't work well concurrently.

My solutions is create a event loop in a single thread, then call `loop.run_forever()`, and rewrite AsyncioExecutor.wait_until_finished as after:

```python
def wait_until_finished(self):
while self.futures:
futures = self.futures
self.futures = []
# self.loop.run_until_complete(wait(futures))
asyncio.run_coroutine_threadsafe(wait(futures), self.loop) # I added this line
```

it works, but this needs to edit the code of graphql-core package, is there a better solution?

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

Read graphql/execution/executors/asyncio.py, especially AsyncioExecutor.wait_until_finished and its loop.run_until_complete call. Reproduce concurrent requests through the Gunicorn setup described in the issue, then determine and test a solution where concurrent requests do not raise RuntimeError: This event loop is already running.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
graphql, python
Domaine
api, backend
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.