graphql-python / graphql-python/graphql-core-legacy
Some troubles using AsyncioExecutor and gunicorn
- Ngôn ngữ chính
- Python
- Star
- 372
- Fork
- 175
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
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?
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Hướng nghiên cứu
Đọc graphql/execution/executors/asyncio.py, đặc biệt là AsyncioExecutor.wait_until_finished và lệnh gọi loop.run_until_complete của nó. Tái hiện các yêu cầu đồng thời thông qua cấu hình Gunicorn được mô tả trong issue, sau đó xác định và kiểm thử một giải pháp trong đó các yêu cầu đồng thời không phát sinh RuntimeError: This event loop is already running.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- graphql, python
- Lĩnh vực
- api, backend
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 35/100