Python - 定期运行脚本 [英] Python - Run script periodically

查看:52
本文介绍了Python - 定期运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一些脚本想要自动定期运行,比如每两天运行一次.

Right now I have some scripts I want to run automatically and periodically, say, every two days.

我构建的方式看起来像这样.

The way I've structured looks something like this.

脚本 1:

def example1():
    #Some Selenium code

example1()

脚本 2:

def example2():
    #Some more Selenium code

example2()

脚本 3:

import Script1
import Script2

你会注意到,'Script1' 和 'Script2' 已经调用了它们的 main 函数,所以在 'Script3' 中我只需要导入脚本,而不是调用函数(虽然这对我有用,我不确定这是否是一种安全的方法/良好的做法).

As you'll notice, 'Script1' and 'Script2' already call their main function, so in 'Script3' I only need to import the scripts, and not call the functions (although this works for me, I'm not sure whether this is a safe approach/good practice).

我的问题:如果我使用 schedule 每 x 天运行一次Script3",这是否意味着脚本将永远运行,还是每 x 天运行一次然后进入睡眠模式直到它必须再次运行?此外,要运行它需要 PC 始终处于开启状态,对吗?

My question: if I use schedule to run 'Script3' every x days, will this mean the script will run forever, or does it run once every x days and then goes into sleep mode until it has to run again? Also, for it to run it would requiere the PC to be always on, right?

如果是这样,有没有办法让它在电脑关闭的情况下自动定期运行?

If so, is there any way to make it run automatically and periodically even with the PC turned off?

提前致谢!

推荐答案

我不确定这是否是一种安全的方法/好的做法

I'm not sure whether this is a safe approach/good practice

Python 之禅说;显式优于隐式."所以最好使用:

The Zen of Python says; "Explicit is better than implicit." So it is better to use:

 import script1
 import script2

 script1.example1()
 script2.example2()

通常不建议模块在导入时运行代码.导入模块最好没有副作用.

It is generally not recommended for a module to run code on import. Importing a module should preferably not have side effects.

如果我使用schedule

可能有不止一个名为schedule"的程序.能具体点吗?

There are probably more than one program named "schedule". Can you be more specific?

有没有办法让它在电脑关闭的情况下自动定期运行

is there any way to make it run automatically and periodically even with the PC turned off

定期运行脚本的方法不止一种.Linux 等类 UNIX 系统具有 cronatrun.其他系统有自己的方式.

There are more than one way to run a script periodically. UNIX-like systems such as Linux have cron and atrun. Other systems have their own way.

但所有这些只有在机器开启时才有效.在没有额外硬件的情况下,通常无法在 PC 上定期打开机器.除非您可以使用 英特尔管理引擎来竞标.

But all of those only work when the machine is on. Turning a machine on at regular intervals is generally not possible on a PC without extra hardware. Unless you can get the Intel Management Engine to do your bidding.

像 ESP32(可以运行 micropython)这样的微控制器确实具有用于此目的的深度睡眠"模式,但我有点怀疑它们是否可以运行 selenium.:-)

Microcontrollers such as the ESP32 (which can run micropython) do have a "deep-sleep" mode for this purpose but I kind of doubt they can run selenium. :-)

这篇关于Python - 定期运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆