-
-
Notifications
You must be signed in to change notification settings - Fork 25
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
trio_asyncio.open_loop() in a fixture? #71
Comments
Hi, You should have a look here: Btw trio-asyncio is tricky because it creates relationship between two event loops, which can end up in hard to track deadlocks. For instance you should avoid using the |
thanks for the hint! could you very shortly explain why it has to be |
@SillyFreak |
If you have trio-mode enabled, then |
I think we currently can't use |
@lordi No, |
Ok, thanks, that's good to know. |
Did anything change since 2019? I'm using this fixture, and it is working just fine: @pytest.fixture
async def asyncio_loop() -> AsyncIterator[asyncio.events.AbstractEventLoop]:
'''
This fixture must be used by any test that tests code involving `asyncio` (as opposed to `trio`)
code and hence `trio_asyncio` wrappers. Ensure the fixture is active for the entire duration of
the test. A fixture may depend on this on behalf of dependent tests as long as the fixture is a
generator (i.e., uses `yield` instead of `return`).
Unfortunately, relevant tests must depend on this fixture explicitly for these reasons:
- It cannot be a session fixture, as `pytest-trio` expects async fixtures to be function-scoped.
<https://pytest-trio.readthedocs.io/en/stable/reference.html#trio-fixtures>
- It cannot be an autouse fixture, as this would apply it to non-async tests and fail.
<https://github.com/python-trio/pytest-trio/issues/123>
'''
# When a ^C happens, trio sends a `Cancelled` exception to each running task. We must protect
# this one to avoid deadlock if it is cancelled before another coroutine that uses trio-asyncio.
with trio.CancelScope(shield=True): # pyright: ignore[reportCallIssue]
async with trio_asyncio.open_loop() as loop:
yield loop BTW, I ran across #105. Would it make sense to offer another value, say, |
I'm not sure why the original poster ran into trouble, but I agree a fixture like you describe should work fine. I think the shield shouldn't be necessary anymore after some recent improvements to trio-asyncio (there's now a bunch of careful shielding inside
Yeah that seems like a pretty easy win! |
`trio_asyncio` as another mode of the `trio_run` ini file option allows us to run tests with an `asyncio` loop implicitly available to all async tests. python-trio#71
Please consider #146. |
I hoped to be able to do the following for my test cases requiring
trio-asyncio
:However the fixture blows up:
I assume that has to do something with the description of fixture handling here (I admit not having read it in full). It seems rather intricate, so I have no idea how hard this would be to fix. I think it would be a valuable addition, though.
The text was updated successfully, but these errors were encountered: