aio-libs / aio-libs/aiomysql

Automatically handle parallel queries with an async `Lock`

未關閉
#852 4 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
enhancement
主要語言
Python
星號
1.9k
分支
272
PR 合併指標
30 天內沒有已合併 PR

描述

### Is your feature request related to a problem?

When performing two "parallel" queries via `asyncio.gather`, an error is thrown:
```RuntimeError: readexactly() called while another coroutine is already waiting for incoming data```

### Describe the solution you'd like

I have implemented a simple yet effective way to remedy this issue however it's quite "gross", requiring monkey patching as I am unable to perform the required changes in a type safe manner:

```python
class LockedCursor(Cursor):
def get_lock(self) -> Lock:
if not hasattr(self.connection, 'lock'):
self.connection.lock = Lock()

return self.connection.lock

async def execute(self, query_string: str, args: Optional[Iterable[Any]] = None):
async with self.get_lock():
return await super().execute(query_string, args)
```

I recommend this feature to be standard on the base cursor, as it's often time impossible to avoid parallel queries happening at the same time.

### Describe alternatives you've considered

Another option would involve the connection holding an instance of `asyncio.Queue` to orchestrate any parallel queries going out, however I personally believe this is overkill and is not as simple to understand as a lock.

Another option I considered is subclassing each of the `Pool`, `Connection` and `Cursor` class to inject `LockedCursor` when calling `connection.cursor`, however that has proven to be very difficult and full of boilerplate and repetitive code to override the constructor and other methods simply to add a lock.

### Additional context

I am willing to help prepare a PR if this is a solution that the maintainers would approve of. I am also willing to discuss other potential solutions and approaches.

### Code of Conduct

- [X] I agree to follow the aio-libs Code of Conduct

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。