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
badacook
V2EX  ›  Python

Python schedule task

  •  
  •   badacook · 2021-04-08 15:55:04 +08:00 · 1643 次点击
    这是一个创建于 1086 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在执行日常管理任务时,有时想实现一个任务脚本,只正确运行一次,就终止,
    否则就循环执行一定次数,或者隔一定时间间隔重启任务,直到被正确执行一次

    如在 windows 下,设定一个任务计划,就像应 do {} until () 循环一样,
    do 表达式 是执行 python 脚本的语句,until 的终止条件是 python 脚本的 exit code 为 0
    不知道这种调用能否实现,还望大家指点一下 或者有类似的解决方式,谢谢了
    8 条回复    2021-04-09 16:28:11 +08:00
    tisswb
        1
    tisswb  
       2021-04-08 15:59:08 +08:00
    windows 有计划任务这个可以实现,不过有时候得配合一下脚本
    junnplus
        2
    junnplus  
       2021-04-08 16:02:22 +08:00   ❤️ 1
    如果是简单的场景,加一个 retry 装饰器就可以
    j0hnj
        3
    j0hnj  
       2021-04-08 16:04:45 +08:00   ❤️ 1
    est
        4
    est  
       2021-04-08 16:09:54 +08:00
    你都这样问了,那么保险起见,建议 bash 实现。
    clino
        5
    clino  
       2021-04-08 16:39:18 +08:00   ❤️ 1
    badacook
        6
    badacook  
    OP
       2021-04-08 19:26:52 +08:00
    @clino 看了一下 你的解决方法好像比较接近,思路大概就是在主线程里捕获执行脚本的线程句柄 handle,传入类似 GetExitCodeThread()函数,有点儿回调的意思,不知道这种理解对不对
    HelloViper
        7
    HelloViper  
       2021-04-09 11:45:14 +08:00
    from tenacity import retry, stop_after_attempt
    clino
        8
    clino  
       2021-04-09 16:28:11 +08:00
    @badacook 没有这么复杂,os.system() 就会等子进程结束才会返回的。
    听你这么说好像和你的需求不一致。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2873 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:25 · PVG 19:25 · LAX 04:25 · JFK 07:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.