badacook
V2EX  ›  Python

Python schedule task

By badacook at 2021 年 4 月 8 日 · 3079 次点击
在执行日常管理任务时,有时想实现一个任务脚本,只正确运行一次,就终止,
否则就循环执行一定次数,或者隔一定时间间隔重启任务,直到被正确执行一次

如在 windows 下,设定一个任务计划,就像应 do {} until () 循环一样,
do 表达式 是执行 python 脚本的语句,until 的终止条件是 python 脚本的 exit code 为 0
不知道这种调用能否实现,还望大家指点一下 或者有类似的解决方式,谢谢了
8 条回复  •  2021-04-09 16:28:11 +08:00
tisswb
   1
tisswb  
   2021 年 4 月 8 日
windows 有计划任务这个可以实现,不过有时候得配合一下脚本
junnplus
   2
junnplus  
   2021 年 4 月 8 日   ❤️ 1
如果是简单的场景,加一个 retry 装饰器就可以
j0hnj
   3
j0hnj  
   2021 年 4 月 8 日   ❤️ 1
est
   4
est  
   2021 年 4 月 8 日
你都这样问了,那么保险起见,建议 bash 实现。
clino
   5
clino  
   2021 年 4 月 8 日   ❤️ 1
badacook
   6
badacook  
OP
   2021 年 4 月 8 日
@clino 看了一下 你的解决方法好像比较接近,思路大概就是在主线程里捕获执行脚本的线程句柄 handle,传入类似 GetExitCodeThread()函数,有点儿回调的意思,不知道这种理解对不对
HelloViper
   7
HelloViper  
   2021 年 4 月 9 日
from tenacity import retry, stop_after_attempt
clino
   8
clino  
   2021 年 4 月 9 日
@badacook 没有这么复杂,os.system() 就会等子进程结束才会返回的。
听你这么说好像和你的需求不一致。
推荐学习书目
› 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
© 2026 V2EX · 34ms · 3.9.8.5