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

Should event_loop fixture call asyncio.set_event_loop()? #80

Closed
adamrothman opened this issue Mar 25, 2018 · 3 comments
Closed

Should event_loop fixture call asyncio.set_event_loop()? #80

adamrothman opened this issue Mar 25, 2018 · 3 comments

Comments

@adamrothman
Copy link

I use pytest and pytest-asyncio to test a few Tornado applications. I had some trouble (see tornadoweb/tornado#2324) with Tornado getting a different event loop from the one created by pytest-asyncio's event_loop fixture.

This can be fixed by overriding Tornado's default testing behavior to use the "current" loop, rather than creating a new one. The issue is that pytest-asyncio does not call asyncio.set_event_loop with the loop it creates in the event_loop fixture – without which, there is no "current" loop.

I'd like to propose change the event_loop fixture to look like this:

@fixture
def event_loop(request):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    yield loop
    loop.close()
    asyncio.set_event_loop(None)

How does that sound? I'm happy to submit a PR, but I wanted to get feedback on the idea first.

@Tinche
Copy link
Member

Tinche commented Mar 26, 2018

Hm, I think it used to be considered bad form to call asyncio.get_event_loop before, but now it's considered OK, so we might enable this. @asvetlov thoughts?

In any case, we use a fixture hook to do things like this in order to keep the event_loop fixture very simple, so it's simple to override.

@asvetlov
Copy link
Contributor

It is safe starting from Python 3.5.3

@asvetlov
Copy link
Contributor

asvetlov commented Jan 8, 2022

pytest-asyncio operates with async tests only.
The async test always have an event loop available by both asyncio.get_event_loop() and asyncio.get_running_loop() calls.
There is no need to explicitly call asyncio.set_event_loop(), it is done by loop.run_untile_complete() already.

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

No branches or pull requests

3 participants