Python - Run script periodically

寵の児 提交于 2021-01-29 14:14:10

问题


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:

def example1():
    #Some Selenium code

example1()

Script2:

def example2():
    #Some more Selenium code

example2()

Script3:

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).

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?

Thanks in advance!


回答1:


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

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.

if I use 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

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.

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.

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. :-)




回答2:


I suppose Windows scheduler works in the same way as cron jobs in linux. So in fact after creating the "task", windows checks every second to see if he has to do any task. 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.
  2. Windows execute the python script inside the batch file, and the script finishes.
  3. Windows keeps checking the scheduler but the process of the script "died".

For the part of doing it even with the PC turned off, that's not possible.




回答3:


If you are on Linux or any other Unix based OS, then you can use Cron for job scheduling. But if you shutdown while the cron jobs are running, the system shuts down and the cron jobs stop (or don't run).

One alternative you can check into is anacron.



来源:https://stackoverflow.com/questions/61269234/python-run-script-periodically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!