A Python coroutine method is a built-in function bound to a native coroutine object (defined usingDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
async def) that controls its execution state. These methods allow the caller to resume execution, inject data, raise exceptions, or terminate the coroutine by interacting with the iterator returned by the underlying awaitable’s __await__() method. Native coroutines implement the collections.abc.Coroutine protocol and expose specific methods to manage their lifecycle across suspension points (await expressions).
Core Coroutine Methods
coroutine.send(value)
Starts or resumes the execution of the coroutine.
- When a coroutine is first started, it must be primed by calling
send(None). - If the coroutine is suspended at an
awaitexpression, theawaitkeyword delegates to the underlying awaitable’s__await__iterator. Thevaluepassed tosend(value)is injected into this underlying iterator. Theawaitexpression itself evaluates to the iterator’s final return value (extracted fromStopIteration.value), not the value passed tosend(). - The method returns the next value yielded by the coroutine’s internal iterator, or raises
StopIterationif the coroutine returns. TheStopIteration.valueattribute contains the coroutine’s final return value.
coroutine.throw(exc)
Raises the specified exception instance at the exact point where the coroutine is currently suspended.
- If the coroutine catches the exception and yields a new value, that value is returned to the caller.
- If the coroutine does not catch the exception, or if it raises a different exception, the exception propagates back to the caller.
- If the coroutine exits cleanly after catching the exception, it raises
StopIteration.
coroutine.close()
Forces the coroutine to terminate.
- It raises a
GeneratorExitexception at the current suspension point. - If the coroutine catches
GeneratorExitand attempts to yield a new value, aRuntimeErroris raised. - If the coroutine handles the exception and exits, or if it was already closed, the method returns silently.
coroutine.__await__()
Returns an iterator that implements the coroutine protocol. This magic method is invoked internally when the coroutine is used in an await expression, allowing the caller to interface with the coroutine’s send(), throw(), and close() methods.
Syntax and Internal Mechanics
While these methods are typically abstracted away by an event loop, they can be invoked directly to manually step through a coroutine’s state machine. To demonstrate manual stepping without an active event loop, a custom dummy awaitable is required.State Transitions
Coroutine methods directly manipulate the internal state of the coroutine object, transitioning it through four primary phases:CORO_CREATED: The coroutine object is instantiated butsend(None)has not yet been called.CORO_RUNNING: The coroutine is actively executing on the thread (cannot be stepped concurrently).CORO_SUSPENDED: The coroutine has yielded control back to the caller via anawaitexpression and is waiting forsend()orthrow().CORO_CLOSED: The coroutine has returned a value, raised an unhandled exception, orclose()has been called.
Master Python with Deep Grasping Methodology!Learn More





