A normal function runs from start to finish once called. A coroutine can stop in the middle, let something else run, and later continue where it left off. Programs that wait a lot, such as web servers waiting for network replies, use coroutines to handle many tasks at once in a single thread.
Subroutines and coroutines
Subroutine (ordinary function)
Coroutine
Entry
One entry point; always starts at the top
Can be resumed at each point where it suspended
Suspension
Cannot pause; runs to the end, then returns
Can pause (yield or await) and resume later
Control
The caller waits until it returns
Gives control back voluntarily, then continues
Coordination
A main function calls subroutines in order
Coroutines pass control among themselves, often through a scheduler (event loop)
State between calls
Local variables are lost on return
Local variables are kept while suspended
Cooperative multitasking, threads and coroutines
In cooperative multitasking, each task voluntarily gives up control when it reaches a waiting point, so another task can run. Nothing interrupts a task against its will.
Threads
Coroutines
Who switches
The operating system’s scheduler, at any moment (preemptive)
The program itself, only at yield/await points (cooperative)
Number of OS threads
One per thread
Usually all in one thread
Cost
Heavier: each has its own stack, managed by the OS
Very light: thousands are cheap
Data-sharing risks
Switches can happen mid-operation; locks are often needed
Switches happen only at known points; fewer race conditions
Best for
CPU work in other languages; blocking libraries
Many tasks that mostly wait (network, files, timers)
Coroutines give concurrency (many tasks in progress, taking turns), not parallelism (tasks running at the same instant on different cores).
Generator-based coroutines: send()
Before async/await existed, Python coroutines were built from generators. Besides producing values with yield, a generator can receive values: value = yield pauses, and gen.send(x) resumes it with value set to x. A coroutine must first be started (primed) with next(gen) so that it reaches its first yield.
Key takeaways
Coroutines generalise subroutines: they can suspend and resume.
Cooperative multitasking: tasks hand over control voluntarily.
Threads are switched by the OS; coroutines switch only at yield/await.
Generator coroutines receive data with send() after priming with next().
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Compare subroutines and coroutines in three points.
Show worked answer
(1) Entry: a subroutine has one entry point and always starts from the beginning, while a coroutine can be resumed at each point where it previously suspended. (2) Suspension: a subroutine cannot pause; it runs to completion and returns, losing its local variables, while a coroutine can suspend (with yield or await), keep its local state and continue later. (3) Control: subroutines are called and coordinated by a main function, and the caller waits for each to finish; coroutines pass control to one another (or to an event loop) voluntarily, which allows cooperative multitasking.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/python-advanced-concepts/coroutines-and-cooperative-multitasking/ to read this page.