Skip to content

fix: resolve Python 3.14 deprecation warnings for asyncio event loop policy#3147

Open
r266-tech wants to merge 1 commit into
sanic-org:mainfrom
r266-tech:fix/python314-deprecation-warnings
Open

fix: resolve Python 3.14 deprecation warnings for asyncio event loop policy#3147
r266-tech wants to merge 1 commit into
sanic-org:mainfrom
r266-tech:fix/python314-deprecation-warnings

Conversation

@r266-tech
Copy link
Copy Markdown

Summary

Fixes #3129

Replaces deprecated asyncio.get_event_loop_policy() and asyncio.set_event_loop_policy() calls with sys.version_info checks that use asyncio.set_event_loop_factory() on Python 3.12+ where available.

Changes

sanic/server/loop.py

  • try_use_uvloop(): On Python 3.12+ with set_event_loop_factory available, use it instead of the deprecated policy API. Falls back to policy API for older Python versions.
  • try_windows_loop(): Same pattern for Windows selector event loop.

sanic/app.py

  • get_loop(): Replace asyncio.get_event_loop_policy().get_event_loop() with asyncio.new_event_loop() to avoid deprecated policy API.

Compatibility

  • Python 3.10-3.11: Uses existing asyncio.set_event_loop_policy() API (not deprecated in these versions)
  • Python 3.12-3.13: Uses policy API (not yet deprecated)
  • Python 3.14+: Uses asyncio.set_event_loop_factory() (avoids deprecation warnings)

Testing

The fix uses hasattr(asyncio, 'set_event_loop_factory') as a feature-detection fallback, so it's safe even if the attribute is unexpectedly unavailable.

…policy

Replace deprecated asyncio.get_event_loop_policy() and
asyncio.set_event_loop_policy() calls with sys.version_info checks
that use asyncio.set_event_loop_factory() on Python 3.12+ where
available.

Also replace asyncio.get_event_loop_policy().get_event_loop() in
app.py with asyncio.new_event_loop() to avoid deprecated policy API.

Fixes sanic-org#3129
@r266-tech r266-tech requested a review from a team as a code owner March 23, 2026 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python 3.14 deprecation warnings for asyncio.get_event_loop_policy still present in 25.12.0

1 participant