How to fix aiopg exception on initialization engine in aiohttp application?

454 views Asked by At

I'm studying aiohttp using official documentation and on step where I must get database connection the code raises exception. I have a function and call it with: app.on_startup.append(function) like in the documentation. Function's code is below:

conf = app['config']['postgres']
engine = await aiopg.sa.create_engine(
    database=conf['database'],
    user=conf['user'],
    password=conf['password'],
    host=conf['host'],
    port=conf['port'],
    minsize=conf['minsize'],
    maxsize=conf['maxsize'],
)
app['db'] = engine

Exception below:


Traceback (most recent call last):
  File "chapi_pools\main.py", line 14, in 
    web.run_app(app)
  File "C:\coding\Chapi\env\lib\site-packages\aiohttp\web.py", line 419, in run_app
    loop.run_until_complete(_run_app(app,
  File "c:\myprogramm\python\lib\asyncio\base_events.py", line 616, in run_until_complete
    return future.result()
  File "C:\coding\Chapi\env\lib\site-packages\aiohttp\web.py", line 305, in _run_app
    await runner.setup()
  File "C:\coding\Chapi\env\lib\site-packages\aiohttp\web_runner.py", line 232, in setup
    self._server = await self._make_server()
  File "C:\coding\Chapi\env\lib\site-packages\aiohttp\web_runner.py", line 331, in _make_server
    await self._app.startup()
  File "C:\coding\Chapi\env\lib\site-packages\aiohttp\web_app.py", line 389, in startup
    await self.on_startup.send(self)
  File "C:\coding\Chapi\env\lib\site-packages\aiohttp\signals.py", line 34, in send
    await receiver(*args, **kwargs)  # type: ignore
  File "C:\coding\Chapi\chapi_pools\db.py", line 46, in init_pg
    engine = await aiopg.sa.create_engine(
  File "C:\coding\Chapi\env\lib\site-packages\aiopg\sa\engine.py", line 69, in _create_engine
    pool = await aiopg.create_pool(
  File "C:\coding\Chapi\env\lib\site-packages\aiopg\pool.py", line 155, in from_pool_fill
    await self._fill_free_pool(False)
  File "C:\coding\Chapi\env\lib\site-packages\aiopg\pool.py", line 193, in _fill_free_pool
    conn = await connect(
  File "C:\coding\Chapi\env\lib\site-packages\aiopg\connection.py", line 38, in connect
    coro = Connection(
  File "C:\coding\Chapi\env\lib\site-packages\aiopg\connection.py", line 92, in __init__
    self._loop.add_reader(self._fileno, self._ready, self._weakref)
  File "c:\myprogramm\python\lib\asyncio\events.py", line 501, in add_reader
    raise NotImplementedError
NotImplementedError
Exception ignored in: 
Traceback (most recent call last):
  File "C:\coding\Chapi\env\lib\site-packages\aiopg\connection.py", line 512, in __del__
  File "C:\coding\Chapi\env\lib\site-packages\aiopg\connection.py", line 310, in close
  File "C:\coding\Chapi\env\lib\site-packages\aiopg\connection.py", line 285, in _close
  File "c:\myprogramm\python\lib\asyncio\events.py", line 504, in remove_reader
NotImplementedError:

Please, help me. I do not know what to do with it

1

There are 1 answers

0
nwzh On

The issue is because of the windows OS environment.
Copied an solution from the link below: https://github.com/aio-libs/aiopg/issues/678#issuecomment-667908402

Quick workaround is to add this to your code

import sys, asyncio
if sys.version_info >= (3, 8) and sys.platform.lower().startswith("win"):
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())