Python 3.2+inspectfunction
inspect.getgeneratorstate()
coroutines and generators
A generator's state as a string: GEN_CREATED, GEN_RUNNING, GEN_SUSPENDED or GEN_CLOSED.
Syntax
inspect.getgeneratorstate(generator)
States are plain strings, available as the inspect.GEN_* constants; compare against those. GEN_RUNNING is visible only from inside the generator itself. An exhausted generator and one closed with close() are both GEN_CLOSED, while a created but not started one is GEN_CREATED and accepts only send(None). Coroutines have their own getcoroutinestate.
Examples
import inspect
def g():
yield inspect.getgeneratorstate(gen)
gen = g()
print(inspect.getgeneratorstate(gen)) # → GEN_CREATED
print(next(gen)) # → GEN_RUNNING
print(inspect.getgeneratorstate(gen) == inspect.GEN_SUSPENDED) # → True
gen.close()
print(inspect.getgeneratorstate(gen)) # → GEN_CLOSED
See also
Open in the full glossarydocs.python.orgReport an inaccuracy