Skip to content

Conversation

@SAKURA-CAT
Copy link
Member

Description

重构了定时器部分代码,现在封装了一个更通用的定时器类型,为 #1255 做准备

Introduced a new Timer utility in swanlab.core_python.utils for scheduled and repeated task execution using daemon threads and event signaling. Added comprehensive unit tests for Timer, updated requirements-dev.txt to include pytest-mock, and adjusted __init__.py imports to expose the new utility.
Replaced MonitorCron with timer.Timer and a new monitor_interval function for hardware monitoring in SwanLabRun. Removed the check_log_level utility and its test, simplifying helper.py and cleaning up related test code. Also commented out problematic imports in __init__.py to avoid circular dependencies.
@SAKURA-CAT SAKURA-CAT requested a review from Zeyi-Lin December 30, 2025 16:00
@SAKURA-CAT SAKURA-CAT self-assigned this Dec 30, 2025
@SAKURA-CAT SAKURA-CAT added the 💪 enhancement New feature or request label Dec 30, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @SAKURA-CAT, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求重构了项目中的定时器功能,引入了一个更通用、更健壮的 Timer 类,旨在为未来的功能扩展(特别是 #1255)奠定基础。通过将现有的硬件监控逻辑迁移到这个新定时器上,提高了代码的模块化和可维护性,并辅以详尽的单元测试来验证其正确性。

Highlights

  • 通用定时器封装: 引入了一个新的 Timer 类,它封装了通用的定时任务逻辑,支持固定间隔、动态间隔以及立即执行。该类基于 threading.EventDaemon 线程实现,提供了 runcanceljoin 方法以确保任务的完整性和可控性。
  • 重构硬件监控: 原有的 MonitorCron 类已被移除,其功能现在通过新封装的 Timer 类实现。这使得硬件监控的定时逻辑更加统一和易于管理,并支持根据执行次数动态调整监控间隔。
  • 新增单元测试: 为新引入的 Timer 类添加了全面的单元测试,覆盖了基本间隔执行、立即执行、动态间隔、取消中断、错误恢复、重复运行警告和重启能力等多个方面,确保了新组件的健壮性。
  • 代码结构优化: 调整了 swanlab/core_python/__init__.py 中的导入方式,从 * 导入改为显式导入,并引入了新的 timer 模块。同时,删除了 swanlab/data/run/helper.py 中不再使用的 check_log_level 函数及其对应的测试文件,精简了代码库。

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次 PR 对定时器部分进行了重构,封装了一个通用的 Timer 类,这是一个非常好的改进。新的 Timer 类设计清晰,支持固定和动态间隔,并且通过 threading.Event 实现了可靠的停止机制,还考虑了任务执行过程中的异常处理,整体上是健壮和灵活的。

代码库中原有的 MonitorCron 被新的 Timer 替代,使得相关代码 (swanlab/data/run/main.py) 更加简洁。

此外,为新的 Timer 类添加了全面的单元测试 (test/unit/core_python/utils/test_timer.py),覆盖了各种场景,这极大地保证了新模块的质量和稳定性。

我只发现了一个小问题,关于 Timer 类中一个似乎不必要的锁,具体见我的评论。总体来说,这是一次高质量的重构。

Eliminated the threading.Lock previously used for mutual exclusion in the Timer class, as it was not necessary for task execution. The task execution method now directly calls the task without lock protection.
@SAKURA-CAT SAKURA-CAT merged commit 28a09dd into main Dec 31, 2025
5 checks passed
@SAKURA-CAT SAKURA-CAT deleted the feat/timer branch December 31, 2025 05:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

💪 enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants