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.

Two or more calls to run_until_complete causes run_until_complete to not exit.

See original GitHub issue

In the following, I call n function that re-enter run_until_complete. When n=1, the program exits. When n=2, the program never exits. Is it expected?

import asyncio
import functools
import time

import nest_asyncio
nest_asyncio.apply()

def sync(corot):
    """
    Make a synchronous function from an asynchronous one.
    :param corot:
    :return:
    """
    result, = asyncio.get_event_loop().run_until_complete(asyncio.gather(corot))
    return result

async def sync_to_corountine(func, *args, **kw):
    """
    Make a coroutine from a synchronous function.
    """
    try:
        return func(*args, *kw)
    finally:
        # every async needs an await.
        await asyncio.sleep(0)

def main():
    async def background(timeout):
        await asyncio.sleep(timeout)
        print(f"Background: {timeout}")

    loop = asyncio.get_event_loop()
    # Run some bacground work to check we are never blocked
    bg_tasks = [
        loop.create_task(background(i))
        for i in range(10)
    ]



    async def long_running_async_task(result):
        # Simulate slow IO
        print(f"...START long_running_async_task [{result}]")
        await asyncio.sleep(4)
        print(f"...END   long_running_async_task [{result}]")
        return result

    def sync_function_with_async_dependency(result):
        print(f"...START sync_function_with_async_dependency [{result}]")
        result = sync(long_running_async_task(result))
        print(f"...END   sync_function_with_async_dependency [{result}]")
        return result

    # Call sync_function_with_async_dependency
    # One reentrant task is OK
    # Multiple reentrant tasks->fails to exit
    n = 2
    for i in range(n):
       bg_tasks.append(sync_to_corountine(sync_function_with_async_dependency, i))

    # OK
    # bg_tasks.append(long_running_async_task(123))
    # bg_tasks.append(long_running_async_task(456))

    task = asyncio.gather(*bg_tasks)  # , loop=loop)
    loop.run_until_complete(task)

if __name__ == '__main__':
    main()

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
erdewitcommented, Mar 10, 2020

This has been fixed in v1.3, which now uses a design more similar to the earlier 1.0 version.

0reactions
stuz5000commented, Mar 10, 2020

Many thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do most asyncio examples use loop.run_until_complete()?
run_until_complete doesn't block anything. The difference between it and run_forever is that the loop pauses at the completion of the coroutine.
Read more >
Event Loop — Python 3.11.1 documentation
If stop() is called while run_forever() is running, the loop will run the current batch of callbacks and then exit. Note that new...
Read more >
asyncio.run() causes RuntimeError because of loop.close()
There is a workaround of using old-school asyncio.get_event_loop().run_until_complete(future) instead of asyncio.run(future) .
Read more >
18.5.1. Base Event Loop — Python 3.6.3 documentation
This causes run_forever() to exit at the next suitable opportunity (see there ... finally: loop.run_until_complete(loop.shutdown_asyncgens()) loop.close().
Read more >
Guide to Concurrency in Python with Asyncio - integralist
What makes the asyncio event loop so effective is the fact that Python implements it ... loop.run_until_complete(hello_world()) loop.close() ...
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