Created
December 6, 2018 15:11
-
-
Save TimothyFitz/bfbc43260c8cc2d3442dbbaac983e8b2 to your computer and use it in GitHub Desktop.
Monkeypatch my asyncio fix until 0.0.20 releases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from aiomysql.pool import Pool, connect | |
def patch_pool(): | |
"""Monkey patch until aiomysql fixes this bug""" | |
Pool._fill_free_pool = _fill_free_pool | |
# Only change from aiomysql's implementation is to check the reader's exception in addition to at_eof. | |
@asyncio.coroutine | |
def _fill_free_pool(self, override_min): | |
# iterate over free connections and remove timeouted ones | |
free_size = len(self._free) | |
n = 0 | |
while n < free_size: | |
conn = self._free[-1] | |
if conn._reader.at_eof() or conn._reader.exception(): # MONKEY PATCH LINE | |
self._free.pop() | |
conn.close() | |
elif (self._recycle > -1 and | |
self._loop.time() - conn.last_usage > self._recycle): | |
self._free.pop() | |
conn.close() | |
else: | |
self._free.rotate() | |
n += 1 | |
while self.size < self.minsize: | |
self._acquiring += 1 | |
try: | |
conn = yield from connect(echo=self._echo, loop=self._loop, | |
**self._conn_kwargs) | |
# raise exception if pool is closing | |
self._free.append(conn) | |
self._cond.notify() | |
finally: | |
self._acquiring -= 1 | |
if self._free: | |
return | |
if override_min and self.size < self.maxsize: | |
self._acquiring += 1 | |
try: | |
conn = yield from connect(echo=self._echo, loop=self._loop, | |
**self._conn_kwargs) | |
# raise exception if pool is closing | |
self._free.append(conn) | |
self._cond.notify() | |
finally: | |
self._acquiring -= 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment