简体   繁体   中英

Keep python scheduler script running on Windows

Let's say I have a python script like this:

from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler

sched = BackgroundScheduler()

def SchedulerTest():
    file1 = open("C:/Stuff/Stuff2/SchedTest.txt","a") 
    L = [str(datetime.now()) + ' | Testing this scheduler.' + '\n']
    print(L)
    file1.writelines(L)
    file1.close()

sched.add_job(SchedulerTest, 'interval', seconds=15, start_date="2020-04-24 07:30:00", end_date="2021-01-01 10:59:00", id='id_SchedulerTest')

sched.start()

saved in a file called SchedTest.py in this directory: C:\Stuff\Stuff2\ along with a blank text file called SchedTest.txt

The process does work as expected in my Jupyter notebook, so long as I have the notebook actively loaded.

How do I run the process and keep it running on my Windows PC without running the script through my Anaconda Navigator-loaded Jupyter notebook?

The below seem unsuccessful as evidenced by an empty SchedTest.txt file, though no error messages popped up for any of them:
1. Make a file named SchedTest_start.cmd with the following contents then double-click it:

echo "Starting my Scheduler Task"
call "C:\ProgramData\Anaconda3\python.exe" "C:\Stuff\Stuff2\SchedTest.py"
pause


2. Opening cmd and typing python C:\Stuff\Stuff2\SchedTest.py
3. Opening cmd and typing pythonw C:\Stuff\Stuff2\SchedTest.py
4. Opening cmd and typing conda run C:\Stuff\Stuff2\SchedTest.py

UPDATE------------------------------------------------------------------------------
Combining the solution by @Chiheb Nexus plus the comment by @Alex Grönholm worked:
1. Change the script to use a blocking scheduler rather than a background one

from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler

sched = BlockingScheduler()

def SchedulerTest():
    file1 = open("C:/Stuff/Stuff2/bl/SchedTest.txt","a") 
    L = [str(datetime.now()) + ' | Testing this scheduler.' + '\n']
    print(L)
    file1.writelines(L)
    file1.close()

sched.add_job(SchedulerTest, 'interval', seconds=15, start_date="2020-04-24 07:30:00", end_date="2021-01-01 10:59:00", id='id_SchedulerTest')

sched.start()

2. Make a file named SchedTest_start.cmd with the following contents then double-

click it:<br>
echo "Starting my Scheduler Task"
call "C:\ProgramData\Anaconda3\python.exe" "C:\Stuff\Stuff2\SchedTest.py"
pause
  1. Install Python from python.org
  2. Press Win + X and select "Command Prompt"
  3. Run pythonw C:\Stuff\Stuff2\EmailScheduler_PPE.py

Understand the consequences. Only run the script if you trust the libraries you used!

pythonw means to run the script with Python without showing the console window. You may want to use python first to debug. Step 3 may not do anything visually, but the script should be started in the background.

Let's say your script is in C:\Users\YOUR_NAME\project and you've already have a virtualenv ( venv ) under the project folder.

In other words your project folder looks like this:

. 

├── project

├── venv

├──  your_script.py

└──  ... (other files)

What you have to do is to create a .cmd file like this example:

my_scheduler.cmd

echo "Starting my Scheduler Task"
call "C:\Users\YOUR_NAME\project\venv\Scripts\python.exe" "C:\Users\YOUR_NAME\project\your_script.py"
pause

Then finally, open Windows Task Scheduler and add the .cmd file under the basic task submenu.

Also, you may consider that instead of using the venv's Python executable you can use your System Python executable. But it's not recommended to do this . The rule say: Each project should have it's own dependencies and should be run under it's proper virtualenv .

Bonus:

ApScheduler can act weirdly due to this behaviour:

If the execution of a job is delayed due to no threads or processes being available in the pool, the executor may skip it due to it being run too late (compared to its originally designated run time). If this is likely to happen in your application, you may want to either increase the number of threads/processes in the executor, or adjust the misfire_grace_time setting to a higher value

For more informations visit APScheduler documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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