Comments (1)
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)
- 1.5.2 and 1.5.3 broke some current_task detection HOT 2
- 1.5.1-1.5.4 is breaking vaex due to #61 HOT 3
- nested tasks causing deadlock? HOT 1
- Request stuck in ProcessPoolExecutor HOT 4
- feature request: unapply HOT 1
- DeprecationWarning on Python 3.10
- RuntimeError: cannot enter context: <_contextvars.Context object at %> is already entered HOT 1
- ThreadPoolExecutor issue HOT 8
- Release tag for v1.5.6 HOT 1
- Execution of Unit Tests Dead Locked After Upgrade to Python 3.10.7 HOT 3
- Lost Logs HOT 1
- nest_ayncio breaks tests under Python 3.10.9 HOT 2
- nest_asyncio.apply not working unless entire asyncio package is imported HOT 1
- strange hanging behavior with `nest_asyncio` when used in combination with `concurrent.futures `ProcessPoolExecutor` HOT 3
- RFE: is it possible to start making github releases?🤔 HOT 2
- How to share websocket context in quart?
- After `apply()` and `asyncio.run()`, Jupyter kernel cannot execute any code HOT 7
- Collision with AsyncIO HOT 1
- `nest_asyncio` breaks `discord.py`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nest_asyncio.