Python asyncio unable to run multiple tasks properly

Question:

I have the following code snippet that I am expecting it to run two async functions (func1 and func2) concurrently, and:

  • Worker is an infinite loop that keeps fetching items from a global asyncio.Queue instance no matter if the queue is empty or not and simply printing some stuff, and Worker.start() is the method that starts that loop
worker1 = Worker()
worker2 = Worker()
worker3 = Worker()

async def func1():
    print("Running func1...")
    await asyncio.gather(worker1.start(), worker2.start(), worker3.start())
    print("func1 done...")

async def func2():
    print("Running func2...")
    await asyncio.sleep(2)
    print("func2 done...")

async def background_tasks():
    asyncio.create_task(func1())
    asyncio.create_task(func2())

if __name__ == '__main__':
    asyncio.run(background_tasks())

I am expecting the two functions run concurrently and could get some output similar with below:

Running func1...
Running func2...
worker1 printing object
worker2 printing object
worker3 waiting
worker2 printing object
func2 done...
worker1 printing object
worker2 printing object
... (expecting no "func1 done..." because of the infinite loop)

But I actually get result output like this:

Running func1...
Running func2...

Process finished with exit code 0

It seems both of the two functions started but never ended properly, even func2 has no ending output, I am unable to find a solution to this and hoping to get some help, thanks in advance!

Asked By: nonemaw

||

Answers:

The code is asynchronous, so after you have created your two tasks eventually the interpreter will move on to the next line of code, which there is none, so it returns and your two coroutines are destroyed with it. you need to await the results of the tasks prior to exiting the function if you want the coroutines to run until completion.

async def background_tasks():
    task1 = asyncio.create_task(func1())
    task2 = asyncio.create_task(func2())
    await task1
    await task2
Answered By: Alexander
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.