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

Clarify docs about requesting the event_loop fixture #964

Open
tuukkamustonen opened this issue Oct 16, 2024 · 3 comments
Open

Clarify docs about requesting the event_loop fixture #964

tuukkamustonen opened this issue Oct 16, 2024 · 3 comments

Comments

@tuukkamustonen
Copy link

https://pytest-asyncio.readthedocs.io/en/latest/reference/fixtures/index.html#event-loop suggests that it's fine to grab event_loop fixture in a non-async function.

0.21+ migration guide in https://pytest-asyncio.readthedocs.io/en/latest/how-to-guides/migrate_from_0_21.html suggests to convert all sync functions requesting the event_loop fixture into async functions, and then acquire the loop via asyncio.get_running_loop().

Which one is it, and what is the rationale of not depending on the event_loop fixture directly?

@seifertm seifertm added this to the v0.24 milestone Oct 24, 2024
@seifertm
Copy link
Contributor

The use of event_loop is deprecated. We should adjust the docs to reflect that.
The rationale is summarized here:
#706 (comment)

@tuukkamustonen
Copy link
Author

tuukkamustonen commented Dec 2, 2024

Slowly getting back here.

The thing why I'm asking is that currently we do something like:

def some_fixture(event_loop):
    # do some sync stuff etc...
    # then trigger async actions:
    event_loop.run_until_complete(...)

Similar pattern is not possible with asyncio.get_running_loop() because:

This function can only be called from a coroutine or a callback.

Being able to run invocations in the event loop in a sync function is convenient, as we don't have 100% async codebase, and tests are mostly written in sync.

Do you see the use case here @seifertm? Is the advice to convert the usages to async and then asyncio.to_thread() to sync parts, instead?

@seifertm
Copy link
Contributor

seifertm commented Dec 6, 2024

The migration guide suggests converting such tests to async tests and use get_running_loop inside the async test. Following your example:

@pytest_asyncio.fixture()
async def some_fixture():
    # do some sync stuff etc...
    # then trigger async actions:
    event_loop = asyncio.get_running_loop()
    event_loop.run_until_complete(...)

You could possibly even skip asyncio.get_running_loop() in favour of:

@pytest_asyncio.fixture()
async def some_fixture():
    # do some sync stuff etc...
    # then trigger async actions:
    await ...

@tuukkamustonen Do any of those approaches work for your code base?

@seifertm seifertm modified the milestones: v0.24, v0.25 Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants