Scalene运行长时程序:如何定期打印内存使用?
使用Scalene定期监控长时间运行程序的内存使用
CLI 端方案
- 通过
--profile-interval参数指定采样间隔(单位:秒),Scalene会按设定周期输出内存等性能数据:
scalene --cli --profile-interval 10 your_prog.py
- 若需要实时追踪日志,可将输出重定向到文件后用
tail实时查看:
scalene --cli --profile-interval 5 your_prog.py > scalene_memory.log & tail -f scalene_memory.log
- 也可以结合
watch命令(仅Linux/macOS可用),定期刷新Scalene的监控输出:
watch -n 5 "scalene --cli --profile-interval 1 your_prog.py"
GUI 端方案
直接启动Scalene的GUI模式,它会自动持续更新程序的内存、CPU等指标,无需额外配置:
scalene your_prog.py
启动后GUI界面会实时展示长时间运行程序的资源使用变化,方便随时查看动态数据。
代码内触发定期打印
如果需要在程序逻辑中主动控制打印时机,可以调用Scalene的内置API:
import scalene import time def long_running_task(): while True: # 这里写入你的业务代码 scalene.scalene_profiler.print_profile() time.sleep(10) # 每10秒主动打印一次监控数据 long_running_task()
运行时直接使用Scalene启动程序即可:
scalene your_prog.py
内容的提问来源于stack exchange,提问作者Rohan Jain
相关产品推荐
相关产品推荐

