Uvloop: Way to verify that uvloop is indeed being used?

Created on 5 Jun 2019  路  2Comments  路  Source: MagicStack/uvloop

I'm using pytest-asyncio and I'm overriding their event loop creation fixture to install uvloop beforehand and then creating a new asyncio loop.

@pytest.yield_fixture()
def event_loop():
    uvloop.install()
    loop = asyncio.new_event_loop()
    yield loop
    loop.close()

Firstly, I have no idea if this even works, and secondly (even if it does work) I would like to somehow verify that I'm using uvloop after creating a new event loop.

Is there a way to check that a new loop is using uvloop under the hood?

Most helpful comment

To check whether the active loop is the newly installed loop you can perform an isinstance check on it:

>>> asyncio.get_event_loop()
<_UnixSelectorEventLoop running=False closed=False debug=False>
>>> isinstance(_, uvloop.Loop)
False
>>> uvloop.install()
>>> asyncio.get_event_loop()
<uvloop.Loop running=False closed=False debug=False>
>>> isinstance(_, uvloop.Loop)
True

I've personally not used pytest-asyncio, so I'm not sure if your snippet would work there.

All 2 comments

To check whether the active loop is the newly installed loop you can perform an isinstance check on it:

>>> asyncio.get_event_loop()
<_UnixSelectorEventLoop running=False closed=False debug=False>
>>> isinstance(_, uvloop.Loop)
False
>>> uvloop.install()
>>> asyncio.get_event_loop()
<uvloop.Loop running=False closed=False debug=False>
>>> isinstance(_, uvloop.Loop)
True

I've personally not used pytest-asyncio, so I'm not sure if your snippet would work there.

Yep, seems to work great :)

@pytest.yield_fixture()
def event_loop():
    uvloop.install()
    loop = asyncio.new_event_loop()
    assert isinstance(loop, uvloop.Loop)
    yield loop
    loop.close()

Thanks!

Was this page helpful?
0 / 5 - 0 ratings