Skip to content

Instantly share code, notes, and snippets.

@TimothyFitz
Created December 6, 2018 15:11
Show Gist options
  • Save TimothyFitz/bfbc43260c8cc2d3442dbbaac983e8b2 to your computer and use it in GitHub Desktop.
Save TimothyFitz/bfbc43260c8cc2d3442dbbaac983e8b2 to your computer and use it in GitHub Desktop.
Monkeypatch my asyncio fix until 0.0.20 releases.
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