57

I'm building an SMTP server with aiosmtpd and used the examples as a base to build from. Below is the code snippet for the entry point to the program.

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.create_task(amain(loop=loop))
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass

When I run the program, I get the following warning:

server.py:61: DeprecationWarning: There is no current event loop
  loop = asyncio.get_event_loop()

What's the correct way to implement this?

1
  • 6
    Wow! This change in get_event_loop() behavior went under the radar for me. I am one of the maintainers of aiosmtpd, and we'll try to urgently push a new version to handle this. Commented Dec 28, 2022 at 9:05

3 Answers 3

70

Your code will run on Python3.10 but as of 3.11 it will be an error to call asyncio.get_event_loop when there is no running loop in the current thread. Since you need loop as an argument to amain, apparently, you must explicitly create and set it.

It is better to launch your main task with asyncio.run than loop.run_forever, unless you have a specific reason for doing it that way. [But see below]

Try this:

if __name__ == '__main__':
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    try:
        asyncio.run(amain(loop=loop))
    except KeyboardInterrupt:
        pass

Added April 15, 2023:

There is a difference between calling asyncio.run(), which I have done here, and calling loop.run_forever() (as in the original question) or loop.run_until_complete(). When I wrote this answer I did not realize that asyncio.run() always creates a new event loop. Therefore in my code above, the variable loop that is passed to amain will not become the "running loop." So my code avoids the DeprecationWarning/RuntimeException, but it doesn't pass a useful loop into amain.

To correct that, replace the line

asyncio.run(amain(loop=loop))

with

loop.run_until_complete(amain(loop=loop))

It would be best to modify amain to obtain the running event loop inside the function instead of passing it in. Then you could launch the program with asyncio.run. But if amain cannot be changed that won't be possible.

Note that run_until_complete, unlike asyncio.run, does not clean up async generators. This is documented in the standard docs.

Sign up to request clarification or add additional context in comments.

10 Comments

do you use amain because it would be different from main ?
I used amain because that was the name used in the original post. Since in the no actual function was provided, the name has no significance. There was no reason to change it.
Similar answer @ stackoverflow.com/a/68652643/1548275 suggesting asyncio.new_event_loop() to replace obsoleted get_event_loop().
As highlighted by the comments in a similar question, this solution may be thread-unsafe. I opted for this solution instead: stackoverflow.com/a/69314701/3873799
the docs suggest this: asyncio.run(main()) . . . here are the docs: docs.python.org/3/library/asyncio-task.html
|
2

In my case, below code resolve my problem:

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

start_server = websockets.serve(echo, "localhost", 50558)

loop.run_until_complete(start_server)
loop.run_forever()

Comments

-1
async def ws(f):
    pass


async def callback(*args):
    pass


async def create():
    tasks = [
        asyncio.create_task(
            ws(callback)
        )
    ]

    await asyncio.wait(tasks)


if __name__ == "__main__":
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    loop.run_until_complete(create())

1 Comment

What information does this add that is not present in previous answers? Please edit to explain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.