Error on application startup after adding pg_context to main.py

45 Views Asked by At

I make polls application from docs. Version of PostgreSQL is 15. Using poetry here are my dependencies from pyproject.toml:

python = "^3.11"
aiohttp = "^3.8.5"
aiodns = "^3.0.0"
pyyaml = "^6.0.1"
aiopg = {extras = ["sa"], version = "^1.4.0"}

main.py:

app.cleanup_ctx.append(pg_context) # Added this

db.py

async def pg_context(app):
    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

    yield

    app['db'].close()
    await app['db'].wait_closed()

If I remove app.cleanup_ctx.append(pg_context) from main.py server runs successfully. But when I try to run server python main.py I got such error (see below): What problem can it be?

From traceback: it says exception occurs when I do this engine = await aiopg.sa.create_engine(... in db.py.

(aiohttp-example-py3.11) F:\python\AIOHTTP\polls>python app_polls/main.py
unhandled exception during asyncio.run() shutdown
task: <Task finished name='Task-1' coro=<_run_app() done, defined at C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiohttp\web.py:289> exception=NotImplementedError()>
Traceback (most recent call last):
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiohttp\web.py", line 516, in run_app
    loop.run_until_complete(main_task)
  File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiohttp\web.py", line 323, in _run_app
    await runner.setup()
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiohttp\web_runner.py", line 279, in setup
    self._server = await self._make_server()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiohttp\web_runner.py", line 375, in _make_server
    await self._app.startup()
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiohttp\web_app.py", line 417, in startup
    await self.on_startup.send(self)
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiosignal\__init__.py", line 36, in send
    await receiver(*args, **kwargs)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiohttp\web_app.py", line 539, in _on_startup
    await it.__anext__()
  File "F:\python\AIOHTTP\polls\app_polls\db.py", line 32, in pg_context
    engine = await aiopg.sa.create_engine(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiopg\sa\engine.py", line 94, in _create_engine
    pool = await aiopg.create_pool(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiopg\pool.py", line 300, in from_pool_fill
    await self._fill_free_pool(False)
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiopg\pool.py", line 336, in _fill_free_pool
    conn = await connect(
                 ^^^^^^^^
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiopg\connection.py", line 65, in connect
    connection = Connection(
                 ^^^^^^^^^^^
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiopg\connection.py", line 772, in __init__
    self._loop.add_reader(
  File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\asyncio\events.py", line 530, in add_reader
    raise NotImplementedError
NotImplementedError
Traceback (most recent call last):
  File "F:\python\AIOHTTP\polls\app_polls\main.py", line 11, in <module>
    web.run_app(app)
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiohttp\web.py", line 516, in run_app
    loop.run_until_complete(main_task)
  File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiohttp\web.py", line 323, in _run_app
    await runner.setup()
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiohttp\web_runner.py", line 279, in setup
    self._server = await self._make_server()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiohttp\web_runner.py", line 375, in _make_server
    await self._app.startup()
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiohttp\web_app.py", line 417, in startup
    await self.on_startup.send(self)
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiosignal\__init__.py", line 36, in send
    await receiver(*args, **kwargs)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiohttp\web_app.py", line 539, in _on_startup
    await it.__anext__()
  File "F:\python\AIOHTTP\polls\app_polls\db.py", line 32, in pg_context
    engine = await aiopg.sa.create_engine(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiopg\sa\engine.py", line 94, in _create_engine
    pool = await aiopg.create_pool(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiopg\pool.py", line 300, in from_pool_fill
    await self._fill_free_pool(False)
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiopg\pool.py", line 336, in _fill_free_pool
    conn = await connect(
                 ^^^^^^^^
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiopg\connection.py", line 65, in connect
    connection = Connection(
                 ^^^^^^^^^^^
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiopg\connection.py", line 772, in __init__
    self._loop.add_reader(
  File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\asyncio\events.py", line 530, in add_reader
    raise NotImplementedError
NotImplementedError
Exception ignored in: <function Connection.__del__ at 0x000001CF28EEC360>
Traceback (most recent call last):
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiopg\connection.py", line 1188, in __del__
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiopg\connection.py", line 995, in close
  File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\aiohttp-example-zDtRzW9K-py3.11\Lib\site-packages\aiopg\connection.py", line 977, in _close
  File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\asyncio\events.py", line 533, in remove_reader
NotImplementedError:
1

There are 1 best solutions below

1
On

Dont know whether it is a full traceback or not, but according to this there is Not Implemented Exception.

Might be because of:

  1. The package version is not compatible with python version.
  2. The function you are calling is not implemented.

Some searching lead me to this. With referance to github comment, you can use this code(might help, have not recreated):

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