Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Coroutine

Корутина

A coroutine is a computation that can pause before it finishes and later pick up where it left off. It keeps the state it needs to continue, such as local variables.

Also known as: сопрограмма

What is a coroutine?

Call an ordinary function twice and it starts from the beginning each time. A coroutine can instead hand back control before it is finished. When resumed, it picks up at the suspension point with its local state intact. For example, a server handler can pause while waiting for a database reply, letting its thread handle another request in the meantime.

How suspension works

A coroutine can suspend at points supported by its language or runtime. In asynchronous code, await often marks a possible suspension point: if the result is not ready, the coroutine can give control back to its scheduler. If the result is already available, execution may continue without a pause. Some coroutine models let the caller resume a coroutine directly. In Python's asyncio, the event loop schedules ready tasks, and a task drives its coroutine forward.

A coroutine is not an operating-system thread. Several coroutines can take turns on one thread, making progress concurrently even though only one is executing there at any instant. Running in parallel on multiple cores requires a runtime and execution setup that support it; coroutine syntax alone does not provide parallelism.

Example: overlapping waits in Python

import asyncio

async def report(name):
    print(name, 'start')
    await asyncio.sleep(1)
    print(name, 'done')

async def main():
    await asyncio.gather(report('A'), report('B'))

asyncio.run(main())

Each call to report creates a coroutine object without running its body. gather schedules both coroutines and returns an awaitable that, in this example, lets main wait for both to finish. Both start lines appear before either done line. The timers overlap, so the program takes about one second rather than two. Each coroutine retains its own name across the pause.

Where coroutines help and what interviewers ask

Coroutines fit workloads with lots of waiting, such as serving network requests or consuming asynchronous data streams. They let you write the steps of one operation in order rather than splitting the logic across callbacks. They do not make a tight computation loop run faster or turn a blocking library call into a nonblocking one.

A common interview exercise is to replace await asyncio.sleep(1) with time.sleep(1). The latter blocks the event loop's thread, so the other task cannot advance during the sleep. Another question is whether await report('A') followed by await report('B') overlaps the waits. It does not: the second call isn't reached until the first finishes. The key distinction is that await waits for work; it does not, by itself, schedule multiple operations to run concurrently.