V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
s04
V2EX  ›  Python

我这个程序出了什么问题,求大佬指教

  •  
  •   s04 · 2020-01-14 14:39:42 +08:00 · 2605 次点击
    这是一个创建于 1536 天前的主题,其中的信息可能已经有所发展或是发生改变。
    本人想写一个内存占用率记录的脚本,这个脚本的目标是将内存占用率大于百分之 90 的情况下的时间戳用 txt 文本记录下来。但本人目前用列表写出来的并不能执行记录功能(由 warning_recorder 函数实现),求大佬指教。

    代码如下:

    <script src=".js"></script>
    7 条回复    2020-01-20 09:46:59 +08:00
    jyyx
        1
    jyyx  
       2020-01-14 17:02:48 +08:00   ❤️ 1
    函数 memory_panel 死循环, 然后打 exit 的时候是直接退出程序
    最后 2 行代码不会执行
    wangyzj
        2
    wangyzj  
       2020-01-14 17:25:38 +08:00   ❤️ 1
    memory_panel 自己绕死了么不是
    krixaar
        3
    krixaar  
       2020-01-14 18:46:51 +08:00   ❤️ 1
    mikeguan
        4
    mikeguan  
       2020-01-14 20:39:46 +08:00 via Android   ❤️ 1
    感觉很多问题啊
    occ_warning_recorder 参数没有使用
    occ_warning_recorder 里面的 for 循环 rate 值一直没变
    occ_warning_recorder 循环里面打开文件
    if-elif 这样使用看着也别扭
    s04
        5
    s04  
    OP
       2020-01-15 15:44:34 +08:00
    @krixaar 大佬厉害了,我仔细研究一下,你的代码里我还有好多看不懂。QAQ
    s04
        6
    s04  
    OP
       2020-01-19 22:59:12 +08:00
    @krixaar 大佬,我初步有点眉目了,但我不太明白如何用你的 APScheduler,如何调度多个函数间隔时间运行?是用 BackgroundScheduler()方法吗?
    krixaar
        7
    krixaar  
       2020-01-20 09:46:59 +08:00   ❤️ 1
    同一个 Scheduler 可以添加多个任务,通过 add_job(要执行的函数, 触发时间, 时间参数) 添加进去之后执行即可。
    触发时间有一次性执行的'date'方式,固定时间间隔的'interval'方式和通过 cron 表达式的'cron'方式。

    BlockingScheduler 启动后作为前台程序,在它 start 之后的代码会被阻塞掉不会被执行,如同你之前代码里的 memory_panel 阻塞掉了后两行导致没法记录。

    BackgroundScheduler 启动后在后台静默执行,不会阻塞之后的代码。

    例如修改为后台执行的样子:
    https://gist.github.com/Raka-loah/07eebf739c6273432ec30b1073c8d63b

    APScheduler 搜一搜有不少教程,具体的自己研究吧。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5392 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:32 · PVG 16:32 · LAX 01:32 · JFK 04:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.