Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefaultEventLoopPolicy.get_event_loop() is deprecated (in the case of no loop) #2178

Closed
vytas7 opened this issue Oct 15, 2023 · 3 comments · Fixed by #2216
Closed

DefaultEventLoopPolicy.get_event_loop() is deprecated (in the case of no loop) #2178

vytas7 opened this issue Oct 15, 2023 · 3 comments · Fixed by #2216
Milestone

Comments

@vytas7
Copy link
Member

vytas7 commented Oct 15, 2023

As of Python 3.12, if there is no event loop, DefaultEventLoopPolicy.get_event_loop() emits a deprecation warning, and threatens to raise an error in future Python versions.

No replacement is suggested by the official docs. When it does start raising an error, I suppose one can catch it, and create a new loop instead.

We have already updated our code to use this method to combat another deprecation... it seems Python wants to make it harder and harder obtaining the current loop from sync code (which makes no sense to me).

@vytas7 vytas7 added this to the Version 4.1 milestone Oct 15, 2023
@CaselIT
Copy link
Member

CaselIT commented Dec 4, 2023

seems that what they want you to do for #reasons is the following

try:
  loop = asyncio.get_running_loop()
except RuntimeError: # lol custom exception are for noobs
  loop = asyncio.new_event_loop()
  asyncio.set_event_loop(loop)

I have no idea how someone looked at this and thought "yeah that's a great api to replace get_event_loop" -.-'

@CaselIT
Copy link
Member

CaselIT commented Dec 4, 2023

seems that what they want you to do for #reasons is the following

try:
  loop = asyncio.get_running_loop()
except RuntimeError: # lol custom exception are for noobs
  loop = asyncio.new_event_loop()
  asyncio.set_event_loop(loop)

I have no idea how someone looked at this and thought "yeah that's a great api to replace get_event_loop" -.-'

nope that's too simple. the following seems correct

def get_event_loop() -> asyncio.AbstractEventLoop:
    try:
        return asyncio.get_running_loop()
    except RuntimeError:
        # avoid "During handling of the above exception, another exception..."
        pass
    if sys.version_info < (3,12):
        return asyncio.get_event_loop()
    else:
        with warnings.catch_warnings(action='error'):
            loop = None
            try:
                loop = asyncio.get_event_loop()
            except (DeprecationWarning, RuntimeError):
                # avoid "During handling of the above exception,
                # another exception..."
                pass
            if loop is None:
                loop = asyncio.new_event_loop()
                asyncio.set_event_loop(loop)
            return loop

@vytas7
Copy link
Member Author

vytas7 commented Dec 5, 2023

See also the discussion on python/cpython#100160

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 a pull request may close this issue.

2 participants