← All notes

Python graphlib TopologicalSorter: order tasks and detect cycles

Answer: Use TopologicalSorter.static_order() when you need an immediate sorting of the nodes in the graph and no parallelism. TopologicalSorter provides functionality to topologically sort a graph of hashable nodes. Its prepare() method raises CycleError (a subclass of ValueError) if cycles exist.

Examples

from graphlib import TopologicalSorter, CycleError

graph = {"D": {"B", "C"}, "C": {"A"}, "B": {"A"}}
ts = TopologicalSorter(graph)
tuple(ts.static_order())        # ('A', 'C', 'B', 'D')

# Parallel processing as nodes become ready
ts = TopologicalSorter()
ts.add("build", "fetch", "configure")
ts.add("test", "build")
ts.prepare()
while ts.is_active():
    for node in ts.get_ready():
        print("run", node)
        ts.done(node)

try:
    TopologicalSorter({"a": {"b"}, "b": {"a"}}).prepare()
except CycleError as err:
    print("cycle:", err.args[1])

How it works

The parallel API

  1. Create a TopologicalSorter and add nodes.
  2. Call prepare() to mark the graph finished and check for cycles.
  3. While is_active() is True, process the nodes from get_ready().
  4. Call done(node) as each node finishes, which can release new ready nodes.

Rules

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, graphlib - Functionality to operate with graph-like structures; python/cpython (GitHub), Lib/graphlib.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, graphlib - Functionality to operate with graph-like structures; python/cpython (GitHub), Lib/graphlib.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.

Cite this

Permanent ID: iil:note:z3hacdhx

iinventedlight. 2026. "Python graphlib TopologicalSorter: order tasks and detect cycles." iinventedlight Library, iil:note:z3hacdhx, version of 2026-11-18. https://iinventedlight.com/library/notes/python-graphlib/. License: CC-BY-4.0.

@misc{iil_note_z3hacdhx,
  author = {{iinventedlight}},
  title = {{Python graphlib TopologicalSorter: order tasks and detect cycles}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-18},
  url = {https://iinventedlight.com/library/notes/python-graphlib/},
  note = {Permanent ID iil:note:z3hacdhx. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-graphlib.md.