Skip to content

Commit 1d4d60b

Browse files
fix: replace deprecated asyncio.get_event_loop() with modern approach (#1865)
Replace deprecated get_event_loop() with get_running_loop() and fallback to new_event_loop() to fix the "no current event loop" warning in Python 3.10+
1 parent c892d34 commit 1d4d60b

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Changelog
1313
------
1414
Fixed
1515
^^^^^
16+
- Fixed asyncio "no current event loop" deprecation warning by replacing `asyncio.get_event_loop()` with modern event loop handling using `get_running_loop()` with fallback to `new_event_loop()` (#1865)
1617

1718
Changed
1819
^^^^^^^

tortoise/contrib/test/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,12 @@ def initializer(
126126
if db_url is not None: # pragma: nobranch
127127
_TORTOISE_TEST_DB = db_url
128128
_CONFIG = getDBConfig(app_label=app_label, modules=_MODULES)
129-
loop = loop or asyncio.get_event_loop()
129+
if not loop:
130+
try:
131+
loop = asyncio.get_running_loop()
132+
except RuntimeError:
133+
loop = asyncio.new_event_loop()
134+
asyncio.set_event_loop(loop)
130135
_LOOP = loop
131136
loop.run_until_complete(_init_db(_CONFIG))
132137
_CONNECTIONS = connections._copy_storage()

0 commit comments

Comments
 (0)