[Python] RuntimeError: await wasn't used with future when using twisted, pytest_twisted...

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm running into the RuntimeError: await wasn't used with future in a simple pytest. I have the pytest_twisted plugin enabled with the argument --reactor asyncio. I can see that twisted is using asyncio and all my twisted tests run fine. However, this code gives me the above error.

    async def _sleep():
    await asyncio.sleep(1.0)


    @defer.inlineCallbacks
    def test_sleep():
    yield defer.ensureDeferred(_sleep())


    It's just a simple test case to see if I can mix asyncio and twisted code together. The full stack trace is as follows:

    Traceback (most recent call last):
    File "test/test_simple.py", line 23, in test_sleep
    yield defer.ensureDeferred(_sleep())
    File "/usr/local/lib/python3.10/site-packages/twisted/internet/defer.py", line 1697, in _inlineCallbacks
    result = context.run(gen.send, result)
    File "test/test_cli.py", line 18, in _sleep
    await asyncio.sleep(1.0)
    File "/usr/local/lib/python3.10/asyncio/tasks.py", line 605, in sleep
    return await future
    RuntimeError: await wasn't used with future


    Anything obvious jump out?

    Twisted: 22.10.0
    Python: 3.10.11
    pytest: 7.3.1
    pytest_twisted: 1.14.0

    Continue reading...

Compartilhe esta Página