Skip to content

possible contradictions in event_loop documentation #288

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

Closed
ppena-LiveData opened this issue Feb 8, 2022 · 3 comments · Fixed by #375
Closed

possible contradictions in event_loop documentation #288

ppena-LiveData opened this issue Feb 8, 2022 · 3 comments · Fixed by #375

Comments

@ppena-LiveData
Copy link

I'm confused by these two parts of the event_loop section of the README. This wording:

Simply using this fixture will not set the generated event loop as the default asyncio event loop, or change the asyncio event loop policy in any way. Use pytest.mark.asyncio for this purpose.

seems to contradict this other wording that is a little further down in the same section:

If the pytest.mark.asyncio marker is applied, a pytest hook will ensure the produced loop is set as the default global loop. Fixtures depending on the event_loop fixture can expect the policy to be properly modified when they run.

@seifertm
Copy link
Contributor

seifertm commented Feb 9, 2022

Thanks for bringing this up!

I'd say some of that is no longer incorrect and we should rewrite this. Let me try to clear up the confusion:

  1. pytest-asyncio will never change the event loop policy
  2. If an async function is decorated with pytest.mark.asyncio, the plugin will create a dedicated event loop based on the current loop policy and run the async function in that event loop.
  3. You can redefine the event_loop fixture to use the provided loop in a broader scope. For example, changing its scope to session will generate one event loop for the whole test run rather than one event loop per test.

Does that help?

@rafsaf
Copy link

rafsaf commented May 3, 2022

By the way, in python 3.10 asyncio.get_event_loop() is deprecated
https://docs.python.org/3.10/library/asyncio-eventloop.html#asyncio.get_event_loop

In the README we do have following example:

@pytest.fixture
def event_loop():
    loop = MyCustomLoop()
    yield loop
    loop.close()

In 3.9 I was using (for "session" scope)

@pytest.fixture(scope="session")
def event_loop():
    loop = asyncio.get_event_loop()
    yield loop
    loop.close()

# In 3.10:
# DeprecationWarning: There is no current event loop
#   loop = asyncio.get_event_loop()

Now (3.10+) it's more like that to avoid deprecation warning:

@pytest.fixture(scope="session")
def event_loop():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    yield loop
    loop.close()

IMO mysterious MyCustomLoop may distract newcomers, especially when somebody is just beginning with async features in Python, and if someone is familiar with it, my third example would be enough signal that own logic can be used.

@seifertm
Copy link
Contributor

@rafsaf Thanks for your input!

I agree with your point and I incorporated your feedback in the corresponding PR.

(Note that asyncio.set_event_loop will be called upon fixture setup by pytest-asyncio. There's no need to set it explicitly in the event_loop fixture.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants