← All notes

Register a sys.monitoring callback for execution events

Answer: sys.monitoring is a namespace within the sys module, not an independent module. import sys.monitoring would fail with a ModuleNotFoundError. Import sys and then use sys.monitoring. PEP 669 proposes this API for monitoring Python programs running on CPython at low cost.

The three parts

The monitoring API consists of three components: tool identifiers, events, and callbacks. The namespace exposes the functions and constants that activate and control event monitoring.

Tool identifiers

A tool identifier is an integer and the associated name. Identifiers are integers in the range 0 to 5 inclusive. Before registering or activating events, a tool should choose an identifier and call use_tool_id. Tool identifiers are used to discourage tools from interfering with each other. Multiple tools can operate at the same time.

| sys.monitoring | events | |---|---| | sys.monitoring | Namespace within the sys module | | tool identifiers | Integers in the range 0 to 5 inclusive | | events | Execution events of interest | | callbacks | Called when those events occur |

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 PEPs, PEP 669 – Low Impact Monitoring for CPython; Python docs, sys.monitoring — Execution event monitoring. Public-Domain; PSF-2.0. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python PEPs, PEP 669 – Low Impact Monitoring for CPython; Python docs, sys.monitoring — Execution event monitoring. Public-Domain; PSF-2.0.

Cite this

Permanent ID: iil:note:ay5t6edz

iinventedlight. 2026. "Register a sys.monitoring callback for execution events." iinventedlight Library, iil:note:ay5t6edz, version of 2026-11-19. https://iinventedlight.com/library/notes/python-sys-monitoring/. License: CC-BY-4.0.

@misc{iil_note_ay5t6edz,
  author = {{iinventedlight}},
  title = {{Register a sys.monitoring callback for execution events}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-19},
  url = {https://iinventedlight.com/library/notes/python-sys-monitoring/},
  note = {Permanent ID iil:note:ay5t6edz. 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-sys-monitoring.md.