„Ereignisschleife geschlossen“ mit asyncio/asyncpgPython

Python-Programme
Anonymous
 „Ereignisschleife geschlossen“ mit asyncio/asyncpg

Post by Anonymous »

Ich habe eindeutig ein grundlegendes Missverständnis darüber, wie Async funktioniert. In Python 3.14 wurde Folgendes ausgeschnitten:

Code: Select all

import asyncio
import asyncpg

def adapted_async():
conn = asyncio.run(asyncpg.connect(database='async_test'))
asyncio.run(conn.close())

if __name__ == "__main__":
adapted_async()
... führt zu RuntimeError: Die Ereignisschleife ist geschlossen. Mein Verständnis war, dass asyncio.run() bei jedem Aufruf eine neue Ereignisschleife erstellt hat, aber mein Verständnis war offensichtlich falsch. Wie geht das richtig?
(Dies ist natürlich ein rein synthetisches Beispiel.)
Stack-Trace:

Code: Select all

RuntimeError: Task  got Future  attached to a different loop

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/xof/Documents/Dev/async_test/x.py", line 10, in 
adapted_async()
~~~~~~~~~~~~~^^
File "/Users/xof/Documents/Dev/async_test/x.py", line 6, in adapted_async
asyncio.run(conn.close())
~~~~~~~~~~~^^^^^^^^^^^^^^
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/asyncio/runners.py", line 204, in run
return runner.run(main)
~~~~~~~~~~^^^^^^
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/asyncio/runners.py", line 127, in run
return self._loop.run_until_complete(task)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/asyncio/base_events.py", line 719, in run_until_complete
return future.result()
~~~~~~~~~~~~~^^
File "/Users/xof/Documents/Dev/async_test/.venv/lib/python3.14/site-packages/asyncpg/connection.py", line 1513, in close
await self._protocol.close(timeout)
File "asyncpg/protocol/protocol.pyx", line 640, in close
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/asyncio/selector_events.py", line 830, in abort
self._force_close(None)
~~~~~~~~~~~~~~~~~^^^^^^
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/asyncio/selector_events.py", line 902, in _force_close
self._loop.call_soon(self._call_connection_lost, exc)
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/asyncio/base_events.py", line 827, in call_soon
self._check_closed()
~~~~~~~~~~~~~~~~~~^^
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/asyncio/base_events.py", line 550, in _check_closed
raise RuntimeError('Event loop is closed')

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post