APScheduler 未启动?

发布于 2024-11-04 15:46:07 字数 756 浏览 3 评论 0原文

我想在晚上运行一个 python 脚本,所以我考虑使用 APScheduler。我将在第二天晚上 1 点开始运行它,它将每天晚上运行一次,

我的调度程序脚本如下所示 (scheduler.py):

from apscheduler.scheduler import Scheduler
from datetime import datetime, timedelta, time, date

def myScript():
    print "ok"

if __name__ == '__main__':
    sched = Scheduler()
    startDate = datetime.combine(date.today() + timedelta(days=1),time(1))
    sched.start()
    sched.add_interval_job(myScript, start_date = startDate, days=1)

在 shell 中,我执行以下操作: python myScheduler.py & disown (我正在远程运行它,所以我想在后台运行它并拒绝它。 立即,该行下方会出现一个数字 (PID),就像其他所有 Python 脚本一样。但是当我这样做时 ps -e | grep python,那个数字不存在。我尝试执行 kill -9 PID 并收到一条消息,指出该作业不存在。

调度程序是否正在运行?如果是的话,我该如何阻止它?如果不是,我做错了什么?

I would like to run a python script during the night and so I was thinking of using APScheduler. I'll start running it at 1am of the following night and it will run once every night

my scheduler script looks like this (scheduler.py):

from apscheduler.scheduler import Scheduler
from datetime import datetime, timedelta, time, date

def myScript():
    print "ok"

if __name__ == '__main__':
    sched = Scheduler()
    startDate = datetime.combine(date.today() + timedelta(days=1),time(1))
    sched.start()
    sched.add_interval_job(myScript, start_date = startDate, days=1)

In the shell, I do:
python myScheduler.py & disown (I'm running it remotely, so I want to run it in the background and disown it.
Immediately, a number (PID) appears below the line, as every other python script would do. But when I do ps -e | grep python, that number is not there. I tried to do kill -9 PID and I got a message saying that the job does not exist.

Is the scheduler running? If yes, how can I stop it? if not, what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最舍不得你 2024-11-11 15:46:08

如果您使用2.1.0版本,您还可以将standalone=True参数传递给Scheduler构造函数。详细文档可以在此处找到

from apscheduler.scheduler import Scheduler
from datetime import datetime, timedelta, time, date

def myScript():
    print "ok"

if __name__ == '__main__':
    sched = Scheduler(standalone=True)
    startDate = datetime.combine(date.today() + timedelta(days=1),time(1))
    sched.add_interval_job(myScript, start_date = startDate, days=1)
    sched.start()

If you use version 2.1.0, you can also pass standalone=True parameter to the Scheduler constructor. Detail documents can be found here

from apscheduler.scheduler import Scheduler
from datetime import datetime, timedelta, time, date

def myScript():
    print "ok"

if __name__ == '__main__':
    sched = Scheduler(standalone=True)
    startDate = datetime.combine(date.today() + timedelta(days=1),time(1))
    sched.add_interval_job(myScript, start_date = startDate, days=1)
    sched.start()
老旧海报 2024-11-11 15:46:08

我认为你应该使用 阻塞调度程序如今。

from apscheduler.schedulers.blocking import BlockingScheduler

scheduler = BlockingScheduler()
# scheduler.add_job(...)
scheduler.add_interval_job(myScript, start_date = startDate, days=1)
scheduler.start()

I think you should use the blocking scheduler nowadays.

from apscheduler.schedulers.blocking import BlockingScheduler

scheduler = BlockingScheduler()
# scheduler.add_job(...)
scheduler.add_interval_job(myScript, start_date = startDate, days=1)
scheduler.start()
只为守护你 2024-11-11 15:46:07

您必须保持脚本运行,否则在 sched.add_interval_job(myScript, start_date = startDate, days=1) 之后,脚本将结束并停止。
添加一个

import time

while True:
    time.sleep(10)
sched.shutdown()

after,然后,调度程序仍然存在。

you have to keep the script running otherwise after the sched.add_interval_job(myScript, start_date = startDate, days=1), the script ends and stop.
add a

import time

while True:
    time.sleep(10)
sched.shutdown()

after, and then, the scheduler will still be alive.

街角卖回忆 2024-11-11 15:46:07

正确的解决方案是告诉调度程序不要作为守护进程运行:

sched = Scheduler()
sched.daemonic = False

或者

sched = Scheduler()
sched.configure({'apscheduler.daemonic': False})

The correct solution would be to tell the scheduler to not run as a daemon:

sched = Scheduler()
sched.daemonic = False

or

sched = Scheduler()
sched.configure({'apscheduler.daemonic': False})
作业与我同在 2024-11-11 15:46:07

我安装了 apscheduler v3,这就是我要做的。

from apscheduler.schedulers.background import BackgroundScheduler  
def mainjob():
    print("It works!")

if __name__ == '__main__':
    sched = BackgroundScheduler()
    sched.start()
    sched.add_job(mainjob, 'interval', seconds=120)
    input("Press enter to exit.")
    sched.shutdown() 

I have apscheduler v3 installed and this is what I would do.

from apscheduler.schedulers.background import BackgroundScheduler  
def mainjob():
    print("It works!")

if __name__ == '__main__':
    sched = BackgroundScheduler()
    sched.start()
    sched.add_job(mainjob, 'interval', seconds=120)
    input("Press enter to exit.")
    sched.shutdown() 
江城子 2024-11-11 15:46:07

这是我的方法:

from apscheduler.scheduler import Scheduler
def mainjob():
    print("It works!")

if __name__ == '__main__':
    sched = Scheduler()
    sched.start()
    sched.add_interval_job(mainjob,minutes=1)
    input("Press enter to exit.")
    sched.shutdown()

here is my way:

from apscheduler.scheduler import Scheduler
def mainjob():
    print("It works!")

if __name__ == '__main__':
    sched = Scheduler()
    sched.start()
    sched.add_interval_job(mainjob,minutes=1)
    input("Press enter to exit.")
    sched.shutdown()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文