Coder Social home page Coder Social logo

Comments (1)

erdewit avatar erdewit commented on May 24, 2024

It's easier to see what happens if the script is rewritten as follows:

import asyncio
from datetime import datetime
import nest_asyncio

nest_asyncio.apply()

async def task(i):
    print(f"[{datetime.now()}] start running task {i}")
    asyncio.run(asyncio.sleep(1))
    print(f"[{datetime.now()}] task {i} ready")
    print(f"[{datetime.now()}] start running task {i + 5}")
    asyncio.run(asyncio.sleep(1))
    print(f"[{datetime.now()}] task {i + 5} ready")

async def parallel():
    tasks = [asyncio.create_task(task(i)) for i in range(5)]
    await asyncio.gather(*tasks, return_exceptions=True)


asyncio.run(parallel())

with the following output:

[2023-11-18 10:54:59.821213] start running task 0
[2023-11-18 10:54:59.821304] start running task 1
[2023-11-18 10:54:59.821341] start running task 2
[2023-11-18 10:54:59.821374] start running task 3
[2023-11-18 10:54:59.821400] start running task 4
[2023-11-18 10:55:00.822808] task 4 ready
[2023-11-18 10:55:00.822876] start running task 9
[2023-11-18 10:55:01.824596] task 9 ready
[2023-11-18 10:55:01.824738] task 3 ready
[2023-11-18 10:55:01.824762] start running task 8
[2023-11-18 10:55:02.826505] task 8 ready
[2023-11-18 10:55:02.826657] task 2 ready
[2023-11-18 10:55:02.826682] start running task 7
[2023-11-18 10:55:03.828482] task 7 ready
[2023-11-18 10:55:03.828613] task 1 ready
[2023-11-18 10:55:03.828636] start running task 6
[2023-11-18 10:55:04.829585] task 6 ready
[2023-11-18 10:55:04.829789] task 0 ready
[2023-11-18 10:55:04.829838] start running task 5
[2023-11-18 10:55:05.831698] task 5 ready

The first 5 tasks are scheduled at the same time and start executing at the same time. But the other 5 tasks only get scheduled when an earlier task has finished. Since task is synchronous, it will block other tasks from scheduling their next tasks. That's why the later tasks don't run in parallel here. They can be run in parallel with asyncio.create_task.

from nest_asyncio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.