pytest-asyncio: Error of "attached to a different loop" appears in 0.11.0 but not in 0.10.0
My testcase get the error of “attached to a different loop” in 0.11.0 but works fine in 0.10.0
The detail is I have the following code in my testcase:
asyncio.get_event_loop().run_in_executor(...)
Is there any related change in 0.11.0?
A simple testcase for reproducing:
import asyncio
import pytest
@pytest.fixture(scope='function')
async def loop():
return asyncio.get_event_loop()
def foo():
return 0
@pytest.mark.asyncio
async def test_async(loop):
await loop.run_in_executor(None, foo)
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 13
- Comments: 28 (14 by maintainers)
Commits related to this issue
- Do not use pytest-asyncio 0.11.0 tests/integration/test_aiopg_connector.py::test_execute_query_simultaneous fails with pytest-asyncio 0.11.0. See https://github.com/pytest-dev/pytest-asyncio/issues/... — committed to procrastinate-org/procrastinate by deleted user 4 years ago
- Do not use pytest-asyncio 0.11.0 tests/integration/test_aiopg_connector.py::test_execute_query_simultaneous fails with pytest-asyncio 0.11.0. See https://github.com/pytest-dev/pytest-asyncio/issues/... — committed to procrastinate-org/procrastinate by deleted user 4 years ago
- Pin pytest-asyncio See https://github.com/pytest-dev/pytest-asyncio/issues/154. — committed to layday/instawow by layday 4 years ago
- Pin pytest-asyncio to 0.10.0 until event_loop priority issue is fixed Awaited issue: https://github.com/pytest-dev/pytest-asyncio/issues/154 Awaited fix: https://github.com/pytest-dev/pytest-asyncio/... — committed to nolar/kopf-fork-from-zalando-incubator by nolar 4 years ago
- test: downgrade pytest-asyncio pytest-asyncio 0.11.0 uses different loops for fixtures and the actual test and also closes event loop in some cases. Downgrade to 0.10.0 until it's fixed. https://git... — committed to marmarek/qubes-core-qrexec by marmarek 4 years ago
- Added unittests for issue #154. — committed to pytest-dev/pytest-asyncio by simonfagerholm 4 years ago
- Put event_loop first among the fixtures of asyncio tests, fixes #154. — committed to pytest-dev/pytest-asyncio by simonfagerholm 4 years ago
@Tinche Sure, I’ll do it tonight
One problem with depending on the
event_loopfixture is that while it works for that reduced test case it fails for my real test case (it just hangs) and I don’t understand why yet.@simonfagerholm I confirm, your changes help in our case 👍
To apply your suggestion to the example code, just add the
event_loopto theloopfixture. Like the following code segment works:@krizex Great, thanks for the help with confirming! I will see if I can create a fix in the hooks later today
No, the fixture is constructed by calling some low level module and the low level module call
asyncio.get_event_loop()to get the event loop. To leverage theevent_loopwe have to introduce a new param in the code path to pass it to the low level module which is not pratical…BTW, it is a backward compatibility issue so we have to fix it in the library side.