---
title: "空闲定时器"
description: "这个模式以前叫作不活跃追踪定时器（Inactiveness Tracker Timer）。"
canonical_url: "https://docs.superdurable.io/zh-Hans/design-patterns/external-interaction/idle-timer/"
source: "docs/i18n/zh-Hans/docusaurus-plugin-content-docs/current/design-patterns/external-interaction/idle-timer.mdx"
locale: "zh-Hans"
language: "python"
---

> 供 AI Agent 使用的全部页面索引：https://docs.superdurable.io/zh-Hans/llms.txt

# 空闲定时器

本页代码为 Python。其他语言：[Go](https://docs.superdurable.io/zh-Hans/design-patterns/external-interaction/idle-timer.go.md) · [Java](https://docs.superdurable.io/zh-Hans/design-patterns/external-interaction/idle-timer.java.md) · [TypeScript](https://docs.superdurable.io/zh-Hans/design-patterns/external-interaction/idle-timer.typescript.md) · [Rust](https://docs.superdurable.io/zh-Hans/design-patterns/external-interaction/idle-timer.rust.md)

这个模式以前叫作不活跃追踪定时器（Inactiveness Tracker Timer）。

当一个动作只应在一段时间没有活动后才发生时，使用这个模式。`InactivenessTrackerFlow` 从 `TrackerStep` 开始，等待 Timer 或 **Active** Channel。如果活动先到达，`TrackerStep` 转回自身，从而开始一个新的 Timer。如果 Timer 先触发，Flow 转到 `ProcessInactiveness`。

**Flow 图：InactivenessTrackerFlow**

**Step 列表**

1. `Tracker`（起始 Step）：Restart the timer on each activity and continue when the timer fires first. 下一步：`ProcessInactiveness`，或 `Tracker`（再次）。
2. `ProcessInactiveness`：Handle a period without activity and complete. 下一步：Flow 完成。

**RPC 列表**

- `record_activity`：发布到 `Active`。

例子: [examples/python/dex_examples/patterns/inactiveness-tracker-timer/inactiveness_tracker_flow.py](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/patterns/inactiveness-tracker-timer/inactiveness_tracker_flow.py)

## 核心 Step

**Python**

```python
class TrackerStep(Step[None]):
    def __init__(self, process_inactiveness: ProcessInactiveness, active_channel: Channel[None]) -> None:
        self.process_inactiveness = process_inactiveness
        self.active_channel = active_channel

    def wait_for(self, context: Context, input: None) -> Wait:
        del context, input
        return Wait.any_of(Timer.by_duration(TRACKER_DURATION), self.active_channel.for_one())

    def execute(self, context: Context, input: None) -> StepDecision:
        del input
        if context.has_timer_fired():
            return go_to(ProcessInactiveness, None)
        return go_to(TrackerStep, None)
```

例子: [examples/python/dex_examples/patterns/inactiveness-tracker-timer/inactiveness_tracker_flow.py](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/patterns/inactiveness-tracker-timer/inactiveness_tracker_flow.py)

## 下一步

- [带结论的空闲定时器](https://docs.superdurable.io/zh-Hans/design-patterns/external-interaction/idle-timer-with-resolution.md)
- [Timer](https://docs.superdurable.io/zh-Hans/concepts/primitives/timer.md)
- [Channel](https://docs.superdurable.io/zh-Hans/concepts/primitives/channel.md)
