fix: resolve Python 3.14 deprecation warnings for asyncio event loop policy#3147
Open
r266-tech wants to merge 1 commit into
Open
fix: resolve Python 3.14 deprecation warnings for asyncio event loop policy#3147r266-tech wants to merge 1 commit into
r266-tech wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3129
Replaces deprecated
asyncio.get_event_loop_policy()andasyncio.set_event_loop_policy()calls withsys.version_infochecks that useasyncio.set_event_loop_factory()on Python 3.12+ where available.Changes
sanic/server/loop.py
try_use_uvloop(): On Python 3.12+ withset_event_loop_factoryavailable, 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(): Replaceasyncio.get_event_loop_policy().get_event_loop()withasyncio.new_event_loop()to avoid deprecated policy API.Compatibility
asyncio.set_event_loop_policy()API (not deprecated in these versions)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.