在 Windows 上保留 python 调度程序脚本 运行

Keep python scheduler script running on Windows

假设我有一个这样的 python 脚本:

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

保存在这个目录下名为SchedTest.py的文件中:C:\Stuff\Stuff2\ 以及一个名为 SchedTest.txt

的空白文本文件

该过程在我的 Jupyter notebook 中确实按预期工作,只要我主动加载了 notebook。

我如何 运行 该过程并将其 运行 保存在我的 Windows PC 上,而不通过我的 Anaconda Navigator 加载的 Jupyter 笔记本 运行 宁脚本?

以下内容似乎不成功,空的 SchedTest.txt 文件证明了这一点,但没有弹出任何错误消息:
1。使用以下内容创建一个名为 SchedTest_start.cmd 的文件,然后双击它:

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


2。打开 cmd 并输入 python C:\Stuff\Stuff2\SchedTest.py
3。打开 cmd 并输入 pythonw C:\Stuff\Stuff2\SchedTest.py
4。打开 cmd 并输入 conda 运行 C:\Stuff\Stuff2\SchedTest.py

更新 - - - - - - - - - - - - - - - - - - - - - - - - - ----------------------------
结合@Chiheb Nexus 的解决方案和@Alex Grönholm 的评论有效:
1. 更改脚本以使用阻塞调度程序而不是后台调度程序

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。使用以下内容创建一个名为 SchedTest_start.cmd 的文件,然后双击

click it:<br>
echo "Starting my Scheduler Task"
call "C:\ProgramData\Anaconda3\python.exe" "C:\Stuff\Stuff2\SchedTest.py"
pause
  1. python.org 安装 Python
  2. 按 Win + X 和 select "Command Prompt"
  3. 运行 pythonw C:\Stuff\Stuff2\EmailScheduler_PPE.py

了解后果。如果您信任所使用的库,则仅 运行 脚本!

pythonw 表示 运行 带有 Python 的脚本而不显示控制台 window。您可能想先使用 python 进行调试。第 3 步可能不会在视觉上执行任何操作,但脚本应该在后台启动。

假设您的脚本在 C:\Users\YOUR_NAME\project 中,并且您的项目文件夹下已经有一个 virtualenv (venv)。

换句话说,您的项目文件夹如下所示:

. 

├── project

├── venv

├──  your_script.py

└──  ... (other files)

您需要做的是创建一个 .cmd 文件,如下例:

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

最后,打开Windows Task Scheduler并在basic task子菜单下添加.cmd文件。

此外,您可以考虑使用系统 Python 可执行文件来代替 venv's Python executable但不建议这样做。规则说:每个项目都应该有它自己的依赖项,并且应该 运行 在它适当的 virtualenv.

奖金:

ApScheduler 由于这种行为可能会表现得很奇怪:

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

有关详细信息,请访问 APScheduler documentation