New event loop is created across different runs in asyncio
See original GitHub issueI think this is not intentional - the implication is that setting new event loop policies will possibly lose the current event loop:
This causes the pytest plugin to have different event loops across fixtures and tests:
import asyncio
import pytest
@pytest.fixture
async def loop():
return asyncio.get_event_loop()
@pytest.mark.anyio
async def test(loop):
assert loop is asyncio.get_event_loop() # fails here
In many cases, the fixture should share the same loop as the test, for example:
import asyncpg
import pytest
@pytest.fixture
async def conn():
return await asyncpg.connect("postgresql:///")
@pytest.mark.anyio
async def test(conn):
assert await conn.fetchval("SELECT 123") == 123
E RuntimeError: Task <Task pending name='Task-5' coro=<run.<locals>.wrapper() running at .../anyio/_backends/_asyncio.py:67> cb=[run_until_complete.<locals>.<lambda>()]> got Future <Future pending cb=[Protocol._on_waiter_completed()]> attached to a different loop
One possible solution is to cache all policy instances under different setups, and switch to use the right one. I’ll create a PR with a more complete test.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:13 (9 by maintainers)
Top Results From Across the Web
Event Loop — Python 3.11.1 documentation
The event loop is the core of every asyncio application. Event loops run asynchronous tasks and callbacks, perform network IO operations, and run...
Read more >Event loop created by asyncio.new_event_loop hangs
I'm using Python 3.5. 1. ensure_future() accepts optional loop parameter, you should pass it to don't clash with default one. Actually I highly ......
Read more >Asyncio Event Loops Tutorial - TutorialEdge.net
In this tutorial we look at the various ways you can define and work with event loops in Asyncio.
Read more >Bridge between asyncio and Tornado
Event loop policy that allows loop creation on any thread. The default asyncio event loop policy only automatically creates event loops in the...
Read more >9.13. AsyncIO Event Loop - Python
You can create multiple threads and run different event loops in each of them. Python will create a default event loop only in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have a plan for this: run async fixtures in a separate thread where the event loop will be kept running. The tricky part is figuring out when to start and stop the event loop. Each back-end needs one thread each. This might also make it feasible to allow class/module/package level async fixtures.
I just pushed the fully refactored pytest plugin to the
testrunner
branch. Feel free to check it out!