【问题标题】:Autorun python script from python从python自动运行python脚本
【发布时间】:2013-07-28 04:04:35
【问题描述】:

我写了一个从APOD下载随机图片的python脚本。我希望能够在 python 中指定更新频率并让 python 自动运行脚本。所以程序看起来像这样:

//first setup
print "how often should the background change in hours?"
updatefrequency = input()
schedule_autorun(updatefrequency)
//run each time
runprogram()

我看到人们使用 windows 任务调度程序来自动运行程序,但我想从 python 设置它。我在 Windows 7 上运行 python 2.7

【问题讨论】:

    标签: python python-2.7 scheduled-tasks


    【解决方案1】:

    时间模块怎么样?

    import time
    timeinseconds = 100000
    while(1):
        time.sleep(timeinseconds)
        runProgram()
    

    如果运行程序所花费的时间超过了你的等待时间,你也可以将它作为一个单独的进程来调用。

    from subprocess import call
    call(['python','runProgram.py'])
    

    或 subprocess.Popen

    【讨论】:

    • hmmmm 也许,这有多准确,脚本在休眠时占用了多少资源?
    • 我不知道该告诉你什么。在资源方面,它需要与保持 python 运行一样多。比较它的时间,我认为只有当你的睡眠时间非常短时它才会成为一个真正的问题。我从未使用其他方法进行任何比较测试。
    • 好吧,好吧,我想我要做的是让 Windows 任务管理器在登录时运行脚本,然后 time.sleep 将处理它。谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    相关资源
    最近更新 更多