1. What Is a Coroutine?
A coroutine is a unit of program execution that can suspend execution, either voluntarily or through a scheduling mechanism, and later resume from the point where it was suspended.
Unlike traditional functions, which usually begin at an entry point and end when they return, a coroutine can save the necessary state during execution, temporarily yield control, and continue running later.
For example, a network request task might go through the following process:
- Initiate the network request.
- Wait for the server to return data.
- Suspend the current coroutine while waiting.
- Execute other ready tasks.
- Resume the coroutine once the network data arrives.
- Continue processing the response result.
The core characteristics of coroutines include:
- Suspendable: execution can be temporarily suspended at a specific point.
- Resumable: after resuming, execution continues from the point where it was last suspended.
- State preservation: necessary state such as local variables and the execution position is retained during suspension.
- Lightweight scheduling: usually managed by the language runtime, a coroutine library, or an executor, and does not necessarily require the operating system to create a separate thread.
- Cooperative execution: many coroutine implementations rely on explicit suspension points or on yielding control.
Note that a coroutine is an execution model; it is not the same as a thread, nor does it necessarily imply parallel execution.
2. Differences Between Coroutines and Ordinary Functions
Both ordinary functions and coroutines can encapsulate a reusable piece of logic, but their execution lifecycles differ.
| Comparison dimension | Ordinary function | Coroutine |
|---|---|---|
| Execution mode | Executes until return after being called | Can be suspended and resumed |
| State preservation | Usually maintained by the call stack | Requires saving resumable execution state |
| Control | The caller usually receives the return result | Control can be yielded at a suspension point |
| Execution lifecycle | A single call usually executes continuously | Can execute across multiple scheduling stages |
| Scheduling | Determined by the call relationship | Can be managed by the runtime or a scheduler |
| Concurrency capability | Usually requires additional mechanisms | Well suited to organizing large numbers of concurrent tasks |
For example, an ordinary function:
def download():
print("开始下载")
data = request_data()
print("下载完成")
return data
In a synchronous call, if request_data() blocks, the current thread usually has to wait for it to return.
Using Python's asynchronous coroutines:
import asyncio
async def download():
print("开始下载")
data = await request_data()
print("下载完成")
return data
Here, await can suspend the current coroutine while waiting for an asynchronous operation, allowing the event loop to schedule other ready tasks.
Note that async def defines a Python asynchronous function. Calling it creates a coroutine object rather than immediately executing the function body.
async def hello():
print("Hello")
coro = hello()
print(coro)
Only when the coroutine is handed to the event loop to run—for example, via asyncio.run(), asyncio.create_task(), or await—does it begin executing.
3. The Core Working Mechanism of Coroutines
The key to a coroutine's ability to suspend and resume lies in the runtime preserving its execution state and rescheduling it at the appropriate time.
1. Coroutine States
A typical coroutine can have the following states:
| State | Meaning |
|---|---|
| Created | Created but not yet started |
| Ready | Ready to run, waiting to be scheduled |
| Running | Currently executing |
| Suspended | Paused, waiting for some condition or event |
| Completed | Execution finished |
| Failed | An unhandled exception occurred during execution |
| Cancelled | Cancelled; the exact state model depends on the implementation |
Different languages may use different state names and state transition rules, but the basic idea is similar.
2. Coroutine State Transitions
The following is a simplified coroutine lifecycle:
flowchart TD
A["Create coroutine"] --> B["Ready"]
B --> C["Running"]
C --> D{"Need to suspend?"}
D -->|"Yes"| E["Suspended"]
E --> F{"Resume condition met?"}
F -->|"No"| E
F -->|"Yes"| B
D -->|"No, execution complete"| G["Completed"]
C --> H["Unhandled exception occurs"]
H --> I["Failed"]
B --> J["Cancel task"]
C --> J
E --> J
J --> K["Cancelled"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style H fill:#FBE9E9,stroke:#D9AAAA,color:#334155
style I fill:#FBE9E9,stroke:#D9AAAA,color:#334155
style J fill:#FBE9E9,stroke:#D9AAAA,color:#334155
style K fill:#E8EDF2,stroke:#B8C3CE,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
This is a conceptual model and does not mean that all languages use exactly the same state enumeration or scheduling strategy.
3. How does a coroutine save its execution state?
When a coroutine is suspended, the runtime must save the information needed to resume execution.
Typical information includes:
- Where execution currently is.
- The values of local variables.
- The context of the current coroutine.
- Asynchronous operations that have not yet completed.
- State related to exception handling and cleanup.
For example:
async def process():
user_id = 1001
user = await fetch_user(user_id)
result = await process_user(user)
return result
When execution reaches the first await, the coroutine may suspend.
To be able to continue execution later, the runtime needs to preserve necessary state such as user_id and the current execution position.
After the asynchronous operation corresponding to fetch_user() completes, the coroutine can resume execution and continue processing user data.
4. Why can coroutines reduce thread overhead?
Traditional threads are usually scheduled by the operating system. Thread switching may involve kernel scheduling, saving and restoring register state, and resources such as thread stacks.
Many coroutine implementations instead use user-mode scheduling:
- Coroutines are managed by the runtime or a coroutine library.
- Multiple coroutines can reuse a small number of threads.
- A coroutine voluntarily yields execution at suspension points.
- The scheduler selects other ready coroutines to run.
This approach can reduce the management cost of a large number of concurrent tasks.
However, note that:
Coroutines do not mean there is no stack space or context overhead at all.
Coroutine implementations vary greatly across languages:
- Some use an independent stack (stackful coroutine).
- Some use a state machine to save local state (stackless coroutine).
- Some use hybrid implementations.
- Some coroutine runtimes still rely on operating system threads for underlying I/O.
IV. Coroutine Scheduling Mechanisms
Coroutines themselves provide the ability to suspend and resume, while the scheduler decides when to run which coroutine.
1. Cooperative Scheduling
Cooperative scheduling requires coroutines to voluntarily yield execution at appropriate points.
Common suspension points include:
awaitwaiting for an asynchronous operation.yieldvoluntarily yielding execution.- Coroutine suspension operations provided by the runtime.
For example:
import asyncio
async def task_a():
for i in range(3):
print("A", i)
await asyncio.sleep(1)
async def task_b():
for i in range(3):
print("B", i)
await asyncio.sleep(1)
async def main():
await asyncio.gather(task_a(), task_b())
asyncio.run(main())
The two coroutines can alternate execution opportunities during their waiting periods.
Pay special attention: asyncio.sleep(1) is an asynchronous wait and will not block the current thread like time.sleep(1) does.
2. Basic Workflow of the Scheduler
Take event-loop-driven asynchronous coroutines as an example:
flowchart TD
A["Create async task"] --> B["Add to ready queue"]
B --> C["Event loop selects task"]
C --> D["Execute coroutine"]
D --> E{"Suspension point encountered?"}
E -->|"No"| F{"Task completed?"}
F -->|"No"| D
F -->|"Yes"| G["Record task result"]
E -->|"Yes"| H["Register wait condition"]
H --> I["Execute other ready tasks"]
I --> J{"Wait condition satisfied?"}
J -->|"No"| I
J -->|"Yes"| K["Re-add task to ready queue"]
K --> C
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style H fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style I fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style J fill:#FFF5DE,stroke:#E8D29A,color:#334155
style K fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
A typical event loop manages ready tasks, timers, I/O events, and task completion notifications.
Note that the event loop is only one common way to schedule coroutines; not all coroutines must be driven by an event loop.
3. Limitations of Cooperative Scheduling
Cooperative scheduling has an important characteristic: if a coroutine does not actively suspend for a long time, it may occupy the current execution thread.
For example:
async def bad_task():
while True:
calculate_heavy_data()
Although the function is declared as async, there is no real suspension point inside it.
If calculate_heavy_data() runs for a long time, the event loop cannot schedule other tasks in a timely manner.
This can cause:
- Other coroutines to be delayed in execution.
- Network requests to respond more slowly.
- Scheduled tasks to fail to run on time.
- The entire event loop to become blocked.
For CPU-intensive tasks, consider using a process pool, thread pool, or other parallel computing solutions.
V. Differences Between Coroutines, Threads, and Processes
Coroutines, threads, and processes can all be used to organize concurrent tasks, but they operate at different levels.
| Comparison dimension | Process | Thread | Coroutine |
|---|---|---|---|
| Management entity | Operating system | Operating system | Usually the runtime or coroutine library |
| Resource isolation | Usually has an independent address space | Threads in the same process share the address space | Depends on the containing process and runtime |
| Scheduling entity | Operating system | Operating system | Usually a user-space scheduler |
| Creation and switching cost | Usually relatively high | Usually lower than processes | Usually relatively low |
| Execution relationship | Can run in parallel | Can run in parallel | Whether they run in parallel depends on the runtime and thread configuration |
| Communication method | IPC, etc. | Shared memory, synchronization primitives | Message passing, shared state, asynchronous queues, etc. |
| Typical use | Service isolation, independent tasks | Multithreaded computation, background work | Large numbers of concurrent I/O tasks |
1. Processes
A process is an important unit of resource allocation and protection in an operating system.
Each process usually has an independent virtual address space, as well as its own file descriptors, memory mappings, and other resources.
Multiple processes can run truly in parallel on a multi-core CPU.
2. Threads
A thread is one of the basic units of execution scheduled by an operating system.
Multiple threads in the same process share the process address space, but each has its own execution context and thread stack.
Multiple threads can run simultaneously on different CPU cores to achieve parallel computation.
3. Coroutines
A coroutine is a lighter-weight execution unit, usually running on top of a thread.
A single thread can host a large number of coroutines, and coroutines switch tasks by suspending and resuming.
However, whether coroutines can run across threads and whether they can execute in parallel depend on the design of the specific runtime.
4. Relationship Among the Three
A typical multi-layer concurrent architecture can be represented as:
flowchart TB
A["Operating System"]
A --> B["Process A"]
A --> C["Process B"]
B --> D["Thread 1"]
B --> E["Thread 2"]
C --> F["Thread 3"]
D --> G["Coroutine A1"]
D --> H["Coroutine A2"]
D --> I["Coroutine A3"]
E --> J["Coroutine B1"]
E --> K["Coroutine B2"]
F --> L["Coroutine C1"]
F --> M["Coroutine C2"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style F fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style G fill:#FFF5DE,stroke:#E8D29A,color:#334155
style H fill:#FFF5DE,stroke:#E8D29A,color:#334155
style I fill:#FFF5DE,stroke:#E8D29A,color:#334155
style J fill:#FFF5DE,stroke:#E8D29A,color:#334155
style K fill:#FFF5DE,stroke:#E8D29A,color:#334155
style L fill:#FFF5DE,stroke:#E8D29A,color:#334155
style M fill:#FFF5DE,stroke:#E8D29A,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
This is a schematic diagram for understanding hierarchical relationships. In actual runtime, a fixed one-to-many mapping is not necessarily used; coroutines may also be allowed to migrate between threads.
6. The Relationship Between Coroutines and Asynchronous Programming
Coroutines and asynchronous programming often appear together, but they are not the same concept.
Asynchronous programming is a programming model, while a coroutine is an execution mechanism that can implement asynchronous programming.
Asynchronous programming focuses on:
- Tasks do not have to block waiting for results.
- Multiple operations can overlap in execution.
- When a task completes, the caller is notified through callbacks, events, or other mechanisms.
Coroutines focus on:
- How to pause the current execution.
- How to save execution state.
- How to resume execution.
- How to organize the lifecycle of tasks.
Common asynchronous programming approaches include:
| Approach | Core Mechanism | Characteristics |
|---|---|---|
| Callback functions | Invoke a callback after the operation completes | Prone to nesting and scattered control flow |
| Promise / Future | Use an object to represent a future result | Convenient for composing asynchronous operations |
| Coroutines | Suspend and resume execution | Control flow resembles synchronous code |
| Event-driven | Trigger processing logic through events | Suitable for event-intensive systems |
Note:
- Coroutines can be used for asynchronous programming, but not all asynchronous operations must use coroutines.
- Asynchronous operations can be implemented through callbacks or Promises.
- Coroutines in some languages can also be used for generators, task scheduling, and control-flow management, not just asynchronous I/O.
Coroutines and Async / Await
In many modern languages, async and await are important syntactic forms for using coroutines.
For example:
import asyncio
async def fetch_data():
await asyncio.sleep(1)
return "data"
async def main():
result = await fetch_data()
print(result)
asyncio.run(main())
Here:
async defdeclares an asynchronous function.- Calling an asynchronous function produces a coroutine object.
awaitwaits for an awaitable object and may suspend the current coroutine.asyncio.run()creates and runs an event loop, driving the top-level coroutine.
await does not mean the current thread is necessarily blocked. For normal asynchronous waiting, the current coroutine is suspended, and the event loop can continue running other tasks.
7. Coroutine Implementations in Different Languages
Coroutines are not a feature unique to any one language. Different languages provide different syntax, scheduling methods, and runtime support.
1. Python: asyncio Coroutines
Python uses async and await to define native asynchronous coroutines.
import asyncio
async def fetch_user(user_id):
await asyncio.sleep(1)
return {"id": user_id, "name": "Alice"}
async def main():
tasks = [
asyncio.create_task(fetch_user(1)),
asyncio.create_task(fetch_user(2)),
asyncio.create_task(fetch_user(3))
]
users = await asyncio.gather(*tasks)
print(users)
asyncio.run(main())
Characteristics:
- Coroutine functions are declared with
async def. - The current coroutine is suspended with
await. - Tasks are created and scheduled with
asyncio.create_task(). - Concurrent tasks are combined using
asyncio.gather()and similar mechanisms. - Coroutines are typically scheduled by a single-threaded event loop, but they can also be combined with thread pools and process pools.
Note that asyncio coroutine scheduling is generally cooperative. A synchronous blocking call made on the event loop thread can block the entire event loop.
2. JavaScript: async Functions and Promises
JavaScript uses async and await to provide coroutine-like asynchronous control flow.
async function fetchUser(id) {
const response = await fetch(`/api/users/${id}`);
return await response.json();
}
async function main() {
const user1 = await fetchUser(1);
const user2 = await fetchUser(2);
console.log(user1, user2);
}
main();
A JavaScript async function returns a Promise.
await pauses the further execution of the current async function and continues processing the result once the Promise is fulfilled or rejected.
Here a distinction needs to be made:
- A Promise is a representation of a future result.
- async / await is syntax for organizing asynchronous control flow.
- The JavaScript engine manages suspension and resumption through internal mechanisms.
- An async function on the ordinary JavaScript main thread does not automatically gain multithreaded parallelism just because it uses
async.
3. Go: Goroutines
Go provides Goroutines as lightweight concurrent execution units.
package main
import (
"fmt"
"time"
)
func worker(id int) {
for i := 0; i < 3; i++ {
fmt.Println("worker", id, i)
time.Sleep(time.Second)
}
}
func main() {
go worker(1)
go worker(2)
time.Sleep(4 * time.Second)
}
Characteristics:
- Goroutines are started with the
gokeyword. - Goroutines are scheduled by the Go runtime.
- Multiple Goroutines can multiplex onto a smaller number of operating system threads.
- The Go runtime can schedule Goroutines to execute on different threads.
- On multicore machines, Goroutines can achieve true parallel execution.
Go's scheduler is usually described through the three core concepts G, M, and P:
| Concept | Meaning |
|---|---|
| G | Goroutine, representing a concurrent execution task |
| M | Machine, representing an operating system thread |
| P | Processor, representing the scheduling resources needed to execute Go code |
The G, M, P model is an important abstraction for understanding the Go scheduler, but its concrete implementation evolves with Go runtime versions.
4. Kotlin: Coroutines
Kotlin supports suspend functions, structured concurrency, and composition of asynchronous tasks through coroutines.
import kotlinx.coroutines.*
suspend fun fetchUser(id: Int): String {
delay(1000)
return "User $id"
}
fun main() = runBlocking {
val user1 = async { fetchUser(1) }
val user2 = async { fetchUser(2) }
println(user1.await())
println(user2.await())
}
Characteristics:
- Use
suspendto declare a suspendable function. - Use
launchto start a coroutine task that does not directly return a result. - Use
asyncto create a coroutine task that returns a result. - Use
await()to obtain the asynchronous computation result. - Use CoroutineDispatcher to determine which kind of execution context the coroutine runs in.