Skip to content
New issue

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

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

Already on GitHub? # to your account

get "DeprecationWarning: There is no current event loop" warning #1

Open
jcyrss opened this issue Jul 11, 2023 · 5 comments
Open

get "DeprecationWarning: There is no current event loop" warning #1

jcyrss opened this issue Jul 11, 2023 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@jcyrss
Copy link

jcyrss commented Jul 11, 2023

Run this script in Python3.10, we will get "DeprecationWarning: There is no current event loop" warning.

That's caused by

loop = asyncio.get_event_loop()

Suggest to use this

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

And, here

class UDPReceiver:

    @staticmethod
    def create(loop, local_addr, server, verbose):
        task = asyncio.Task(loop.create_datagram_endpoint(
            lambda: UDPReceiver(server=server, verbose=verbose),
            local_addr=local_addr, allow_broadcast=True))
        _, udpreceiver = loop.run_until_complete(task)
        return udpreceiver

It creates a Task instance without specifying event loop, that will cause it to use current running loop which does not exists at that time. It will also bring "DeprecationWarning: There is no current event loop" warning.

Suggest to change to this

class UDPReceiver:

    @staticmethod
    def create(loop, local_addr, server, verbose):
        coro = loop.create_datagram_endpoint(
            lambda: UDPReceiver(server=server, verbose=verbose),
            local_addr=local_addr, allow_broadcast=True)

        task = asyncio.Task(coro,loop=loop)

        _, udpreceiver = loop.run_until_complete(task)
        return udpreceiver

The same modification should apply to UDPSender

@danomatika
Copy link
Member

Thanks for the report. I don't see warnings on my system with Python 3.11.3. I will see if there is a workaround.

@danomatika
Copy link
Member

danomatika commented Jul 18, 2023

Ok, in reading the Python 3.11 changelog, I confirm the issue is really down to deprecating the behavior of asynicio.get_event_loop() towards Python 3.12 - 3.14: python/cpython#93453.

Your proposed fix of explicitly creating the event loop before using it should be fine. I don't think anything else is needed since the class instances all start their tasks using the loop in their respective create methods.

@danomatika
Copy link
Member

Latest commit creates the event loop explicitly. Can you test on your system?

@danomatika danomatika self-assigned this Jul 18, 2023
@danomatika danomatika added the bug Something isn't working label Jul 18, 2023
@jcyrss
Copy link
Author

jcyrss commented Aug 2, 2023

Latest commit creates the event loop explicitly. Can you test on your system?

Yeah, sure. But I don't see any changes to the code file 'baton.py'

image

@danomatika
Copy link
Member

Oops. I pushed to our internal Gitlab server. The commit is now pushed here.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants