Fundamentals 6 min read

Mastering Python Generators: Using send, close, and throw Effectively

This article explains Python generators, introduces the send, close, and throw methods, provides clear code examples for each, demonstrates how to combine them, and shows practical scenarios for real‑time data processing.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Mastering Python Generators: Using send, close, and throw Effectively

Basic Concept of Generators

Generators are special iterators that pause execution at a yield expression and resume later, allowing lazy evaluation and efficient iteration. In addition to the basic yield, Python offers send, close, and throw methods to control a generator's lifecycle.

send Method

The send method transmits a value into a generator and resumes its execution, delivering the value to the waiting yield expression.

def echo():
    while True:
        received_value = yield
        print(f"Received: {received_value}")

gen = echo()
next(gen)  # initialize generator
gen.send("Hello")  # Output: Received: Hello
gen.send("World")  # Output: Received: World
gen.close()  # close generator

close Method

The close method terminates a generator, preventing it from receiving further values. Attempting to send a value after closing raises StopIteration.

def echo():
    try:
        while True:
            received_value = yield
            print(f"Received: {received_value}")
    except GeneratorExit:
        print("Generator closed")

gen = echo()
next(gen)
gen.send("Hello")
gen.send("World")
gen.close()
# gen.send("End")  # raises StopIteration

throw Method

The throw method injects an exception into the generator. If the generator catches the exception, it can handle it; otherwise, the exception propagates as StopIteration.

def echo():
    try:
        while True:
            received_value = yield
            print(f"Received: {received_value}")
    except RuntimeError as e:
        print(f"Exception caught: {e}")

gen = echo()
next(gen)
gen.send("Hello")
gen.send("World")
gen.throw(RuntimeError, "An error occurred")
# gen.send("End")  # raises StopIteration

Combined Usage of send, close, and throw

In real applications, these methods are often used together to finely control generator behavior, handling normal data flow, cleanup, and error conditions.

def echo():
    try:
        while True:
            received_value = yield
            print(f"Received: {received_value}")
    except RuntimeError as e:
        print(f"Exception caught: {e}")
    finally:
        print("Cleaning up...")

gen = echo()
next(gen)
gen.send("Hello")
gen.send("World")
gen.throw(RuntimeError, "An error occurred")
gen.close()

Real‑World Scenario: Processing Real‑Time Data Streams

Generators excel in handling large or continuous data streams, such as real‑time sensor input. The example below shows a generator that processes incoming data, simulates work with a sleep, and cleans up when closed.

import time

def process_data():
    try:
        while True:
            data = yield
            print(f"Processing data: {data}")
            time.sleep(1)  # simulate processing time
    except GeneratorExit:
        print("Data processing stopped")

gen = process_data()
next(gen)
gen.send("Data 1")
time.sleep(2)
gen.send("Data 2")
time.sleep(2)
gen.close()

Conclusion

The article demonstrated how to use the send, close, and throw methods of Python generators with concrete code snippets, explained their effects, and illustrated a practical use case for streaming data processing.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GeneratorsThrowSendclose
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.