Skip to content

event_loop_policy for single test #1101

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

Open
Dreamsorcerer opened this issue Apr 24, 2025 · 3 comments
Open

event_loop_policy for single test #1101

Dreamsorcerer opened this issue Apr 24, 2025 · 3 comments

Comments

@Dreamsorcerer
Copy link

It would be good if there was an easy way to change the event_loop_policy for a single test. I can't see any way to do this currently from the documentation (other than moving a single test to it's own module).

In aiohttp, we currently have these fixtures in conftest.py, which I'm looking to remove:

@pytest.fixture
def selector_loop() -> Iterator[asyncio.AbstractEventLoop]:
    policy = asyncio.WindowsSelectorEventLoopPolicy()  # type: ignore[attr-defined]
    asyncio.set_event_loop_policy(policy)

    with loop_context(policy.new_event_loop) as:
        asyncio.set_event_loop(_loop)
        yield _loop


@pytest.fixture
def uvloop_loop() -> Iterator[asyncio.AbstractEventLoop]:
    policy = uvloop.EventLoopPolicy()
    asyncio.set_event_loop_policy(policy)

    with loop_context(policy.new_event_loop) as:
        asyncio.set_event_loop(_loop)
        yield _loop
@seifertm
Copy link
Contributor

That makes sense. The event loop policy support is not in a good state, unfortunately. I think this is closely related to #796.

@seifertm
Copy link
Contributor

seifertm commented Apr 28, 2025

I'm also linking #1032 which argues that this kind of customization should be possible through the async marker.
For example: @pytest.mark.asyncio(policy=…).

It's also worth mentioning that upstream is deprecating the policy system and looking to remove it entirely:
python/cpython#127949

@Dreamsorcerer
Copy link
Author

I think this is closely related to #796.

That one seems to just be concerned with sync tests geting executed multiple times.

I'm also linking #1032 which argues that this kind of customization should be possible through the async marker.
For example: @pytest.mark.asyncio(policy=…).

Yes, this one may end up solving the issue.

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