Coroutine
PronunciationKOH-roo-teen
Definition
A coroutine is a function that can pause in the middle (at an await), let other work run, and resume later where it stopped. Async code is built from coroutines.
Where you hear it
In Python asyncio, Kotlin, JavaScript async functions and discussions about handling many connections.
Examples
Calling a coroutine function gives you a coroutine object; you must await it.
One thread can run thousands of coroutines.
The coroutine waits for the network call and lets other requests run in the meantime.
Common mistake
Forgetting to await. The coroutine never runs and Python only warns that it was never awaited.
Don't confuse with
A thread, which the operating system schedules and can interrupt at any time. A coroutine only pauses where it chooses to.
Say it at work
- In a meeting or chat
Make this a coroutine so it doesn't block the loop.
- In an email or ticket
Don't call blocking code inside a coroutine.
Keep exploring
More in Programming Fundamentals