erdewit / nest_asyncio

Patch asyncio to allow nested event loops
BSD 2-Clause "Simplified" License
693 stars 79 forks source link

Task not executing #3

Closed davidbrochart closed 5 years ago

davidbrochart commented 6 years ago

Say you have a file foo.py:

import asyncio

async def foo():
    print('foo')
    await asyncio.sleep(0)
    print('done')

loop = asyncio.get_event_loop()
task = asyncio.ensure_future(foo())
loop.run_until_complete(task)

And a Notebook with this cell:

import nest_asyncio
nest_asyncio.apply()

import foo

Executing the cell just hangs, foo() is not executed.

erdewit commented 6 years ago

It can be made to work by directly running the coroutine, without ensure_future:

coro = foo()
loop.run_until_complete(coro)

I'll have to look deeper if the combination of ensure_future and run_until_complete of the same task can be made to work.

davidbrochart commented 6 years ago

The problem is that I can't modify this code. Here it is just a minimal example to reproduce the bug.

erdewit commented 6 years ago

This is now fixed in v0.9.2.

davidbrochart commented 5 years ago

Actually I have a slightly more complex case where it still hangs:

import asyncio

async def foo():
    print('foo')
    await asyncio.sleep(0)
    print('done')

loop = asyncio.get_event_loop()
task = asyncio.ensure_future(foo())
finished, unfinished = loop.run_until_complete(asyncio.wait([task], return_when=asyncio.ALL_COMPLETED))
erdewit commented 5 years ago

The code is overhauled in a pretty major way to fix this bug and several other lingering issues.

Thank you for your bug reports, they have been added as Travis test cases.

davidbrochart commented 5 years ago

Everything works fine for me now, thanks a lot for your support!