简体   繁体   English

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

[英]Keep python scheduler script running on Windows

Let's say I have a python script like this:假设我有一个像这样的 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()

saved in a file called SchedTest.py in this directory: C:\Stuff\Stuff2\ along with a blank text file called SchedTest.txt保存在此目录中名为 SchedTest.py 的文件中:C:\Stuff\Stuff2\ 以及名为 SchedTest.txt 的空白文本文件

The process does work as expected in my Jupyter notebook, so long as I have the notebook actively loaded.只要我主动加载了笔记本,该过程就可以在我的 Jupyter 笔记本中按预期工作。

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?如何运行该进程并使其在我的 Windows PC 上运行而不通过我的 Anaconda Navigator 加载的 Jupyter 笔记本运行脚本?

The below seem unsuccessful as evidenced by an empty SchedTest.txt file, though no error messages popped up for any of them:空的 SchedTest.txt 文件证明了以下内容似乎不成功,尽管其中任何一个都没有弹出错误消息:
1. Make a file named SchedTest_start.cmd with the following contents then double-click it: 1. 创建一个名为 SchedTest_start.cmd 的文件,内容如下,然后双击它:

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 2. 打开 cmd 并输入 python C:\Stuff\Stuff2\SchedTest.py
3. Opening cmd and typing pythonw C:\Stuff\Stuff2\SchedTest.py 3. 打开 cmd 并输入 pythonw C:\Stuff\Stuff2\SchedTest.py
4. Opening cmd and typing conda run C:\Stuff\Stuff2\SchedTest.py 4. 打开 cmd 并输入 conda run C:\Stuff\Stuff2\SchedTest.py

UPDATE------------------------------------------------------------------------------更新 - - - - - - - - - - - - - - - - - - - - - - - - - -----------------------------------------
Combining the solution by @Chiheb Nexus plus the comment by @Alex Grönholm worked:将@Chiheb Nexus 的解决方案加上@Alex Grönholm 的评论结合起来工作:
1. Change the script to use a blocking scheduler rather than a background one 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. Make a file named SchedTest_start.cmd with the following contents then double- 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. Install Python from python.orgpython.org 安装 Python
  2. Press Win + X and select "Command Prompt"按 Win + X 和 select “命令提示符”
  3. Run pythonw C:\Stuff\Stuff2\EmailScheduler_PPE.py运行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. pythonw表示使用 Python 运行脚本,而不显示控制台 window。 You may want to use python first to debug.您可能想先使用python进行调试。 Step 3 may not do anything visually, but the script should be started in the background.第 3 步可能不会在视觉上做任何事情,但脚本应该在后台启动。

Let's say your script is in C:\Users\YOUR_NAME\project and you've already have a virtualenv ( venv ) under the project folder.假设您的脚本位于C:\Users\YOUR_NAME\project中,并且您在项目文件夹下已经有一个virtualenv ( venv )。

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:您需要做的是创建一个.cmd文件,如下例所示:

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

Then finally, open Windows Task Scheduler and add the .cmd file under the basic task submenu.最后,打开Windows Task Scheduler ,在basic task子菜单下添加.cmd文件。

Also, you may consider that instead of using the venv's Python executable you can use your System Python executable.此外,您可以考虑使用系统 Python 可执行文件,而不是使用venv's 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 .规则说:每个项目都应该有它自己的依赖项,并且应该在它正确的 virtualenv 下运行

Bonus:奖金:

ApScheduler can act weirdly due to this behaviour:由于这种行为, 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如果这可能发生在您的应用程序中,您可能需要增加执行程序中的线程/进程数,或者将 misfire_grace_time 设置调整为更高的值

For more informations visit APScheduler documentation有关更多信息,请访问APScheduler 文档

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

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