简体   繁体   English

Python - 定期运行脚本

[英]Python - Run script periodically

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.我的结构方式看起来像这样。

Script1:脚本1:

def example1():
    #Some Selenium code

example1()

Script2:脚本2:

def example2():
    #Some more Selenium code

example2()

Script3:脚本3:

import Script1
import Script2

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

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?我的问题:如果我使用schedule每 x 天运行一次“Script3”,这是否意味着脚本将永远运行,或者它是否每 x 天运行一次,然后进入睡眠模式,直到它必须再次运行? Also, for it to run it would requiere the PC to be always on, right?此外,要让它运行,它需要 PC 始终处于开启状态,对吗?

If so, is there any way to make it run automatically and periodically even with the PC turned off?如果是这样,有什么方法可以让它自动并定期运行,即使在 PC 关闭的情况下?

Thanks in advance!提前致谢!

I suppose Windows scheduler works in the same way as cron jobs in linux.我想 Windows 调度程序的工作方式与 linux 中的 cron 作业相同。 So in fact after creating the "task", windows checks every second to see if he has to do any task.所以实际上在创建“任务”之后,windows 每秒都会检查一下他是否需要执行任何任务。 But this is being done always, so this has no impact in performance if that is what you're worried about.但这是一直在做的,所以如果你担心的话,这对性能没有影响。 Then just to clarify:然后只是为了澄清:

  1. Windows sees the batch file scheduled and execute it on time. Windows 看到调度的批处理文件并按时执行。
  2. Windows execute the python script inside the batch file, and the script finishes. Windows 在批处理文件中执行 python 脚本,脚本完成。
  3. Windows keeps checking the scheduler but the process of the script "died". Windows 不断检查调度程序,但脚本的进程“死亡”。

For the part of doing it even with the PC turned off, that's not possible.即使在 PC 关闭的情况下也能做到这一点,这是不可能的。

I'm not sure whether this is a safe approach/good practice我不确定这是否是一种安全的方法/良好的做法

The Zen of Python says; Python之禅说; "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.导入模块最好不要有副作用。

if I use schedule如果我使用时间表

There are probably more than one program named "schedule".可能有不止一个名为“schedule”的程序。 Can you be more specific?你可以说得更详细点吗?

is there any way to make it run automatically and periodically even with the PC turned off有什么方法可以让它自动并定期运行,即使在 PC 关闭的情况下

There are more than one way to run a script periodically.定期运行脚本的方法不止一种。 UNIX-like systems such as Linux have cron and atrun . Linux 等类 UNIX 系统具有cronatrun Other systems have their own way.其他系统有自己的方式。

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.在没有额外硬件的情况下,通常不可能在 PC 上定期打开机器。 Unless you can get the Intel Management Engine to do your bidding.除非你能得到英特尔管理引擎来做你的投标。

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.诸如 ESP32(可以运行micropython )之类的微控制器确实为此目的具有“深度睡眠”模式,但我有点怀疑它们是否可以运行 selenium。 :-) :-)

If you are on Linux or any other Unix based OS, then you can use Cron for job scheduling.如果您使用的是 Linux 或任何其他基于 Unix 的操作系统,那么您可以使用 Cron 进行作业调度。 But if you shutdown while the cron jobs are running, the system shuts down and the cron jobs stop (or don't run).但是,如果您在 cron 作业运行时关闭,系统会关闭并且 cron 作业会停止(或不运行)。

One alternative you can check into is anacron.您可以检查的另一种选择是 anacron。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM