sys.monitoring --- 執行事件監控¶
在 3.12 版被加入.
備註
sys.monitoring 是 sys 模組內的一個命名空間,不是一個獨立的模組,而 import sys.monitoring 將引發ModuleNotFoundError。只需 import sys 然後使用 sys.monitoring。
此命名空間提供對啟動和控制事件監控所需的函式和常數的存取。
當程式執行時,會發生一些能被監控工具關注的事件。sys.monitoring 命名空間提供了在發生欲關注事件時接收回呼的方法。
監控 API 由三個元件組成:
工具識別器¶
工具識別器是一個整數和關聯的名稱。工具識別器用於防止工具相互干擾並允許多個工具同時運作。目前工具是完全獨立的,不能用來互相監控。將來此限制可能會取消。
在註冊或啟動事件之前,工具應選擇一個識別器。識別器是 0 到 5(含)範圍內的整數。
註冊和使用工具¶
- sys.monitoring.use_tool_id(tool_id: int, name: str, /) None¶
必須在使用 tool_id 之前呼叫。tool_id 必須在 0 到 5(含)範圍內。如果 tool_id 正在使用,則引發
ValueError。
- sys.monitoring.free_tool_id(tool_id: int, /) None¶
一旦工具不再需要 tool_id 就應該被呼叫。在釋放 tool_id 之前會呼叫
clear_tool_id()。
- sys.monitoring.get_tool(tool_id: int, /) str | None¶
如果 tool_id 正在使用,則回傳該工具的名稱,否則回傳
None。tool_id 必須在 0 到 5(含)範圍內。
對於事件,虛擬機器對所有 ID 的處理都是相同的,但預先定義了以下 ID,以使工具間的協作更容易:
sys.monitoring.DEBUGGER_ID = 0
sys.monitoring.COVERAGE_ID = 1
sys.monitoring.PROFILER_ID = 2
sys.monitoring.OPTIMIZER_ID = 5
事件¶
支援以下事件:
- sys.monitoring.events.BRANCH_LEFT¶
一個條件分支往左。
It is up to the tool to determine how to present "left" and "right" branches. There is no guarantee which branch is "left" and which is "right", except that it will be consistent for the duration of the program.
- sys.monitoring.events.BRANCH_RIGHT¶
一個條件分支往右。
- sys.monitoring.events.CALL