question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New event loop is created across different runs in asyncio

See original GitHub issue

I think this is not intentional - the implication is that setting new event loop policies will possibly lose the current event loop:

https://github.com/agronholm/anyio/blob/14daa2bad967bf6dd0f96d04ecffe8e383985195/anyio/_backends/_asyncio.py#L71-L86

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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
agronholmcommented, Jun 10, 2020

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.

0reactions
agronholmcommented, Aug 4, 2020

I just pushed the fully refactored pytest plugin to the testrunner branch. Feel free to check it out!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found