简体   繁体   English

如何使Python apscheduler在后台运行

[英]How to make Python apscheduler run in background

I want to make Python apscheduler run in background , here is my code: 我想让Python apscheduler在后台运行,这是我的代码:

from apscheduler.schedulers.background import BackgroundScheduler, BlockingScheduler  
from datetime import datetime  
import logging  
import sys

logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)

def singleton(cls, *args, **kw):
    instances = {}

    def _singleton(*args, **kw):
        if cls not in instances:
            instances[cls] = cls(*args, **kw)
        return instances[cls]

    return _singleton

@singleton
class MyScheduler(BackgroundScheduler):
    pass

def simple_task(timestamp):  
    logging.info("RUNNING simple_task: %s" % timestamp)

scheduler = MyScheduler()
scheduler.start()

scheduler.add_job(simple_task, 'interval', seconds=5, args=[datetime.utcnow()])

when I run the command: 当我运行命令时:

look:Python look$ python itger.py

I just got this: 我刚得到这个:

INFO:apscheduler.scheduler:Scheduler started DEBUG:apscheduler.scheduler:Looking for jobs to run DEBUG:apscheduler.scheduler:No jobs; INFO:apscheduler.scheduler:计划程序已启动DEBUG:apscheduler.scheduler:正在寻找要运行的作业DEBUG:apscheduler.scheduler:没有工作; waiting until a job is added INFO:apscheduler.scheduler:Added job "simple_task" to job store "default" 等待直到添加作业INFO:apscheduler.scheduler:将作业“ simple_task”添加到作业存储“默认”

And ps: 和ps:

ps -e | grep python

I just got 54615 ttys000 0:00.00 grep python 我刚得到54615 ttys000 0:00.00 grep python

My Problem is how to set the code run in background and I can see it's running or it's print log for every 5 secs so the code show? 我的问题是如何将代码设置为在后台运行,我可以看到它每5秒钟运行一次或打印日志,以便显示代码?

BackgroundScheduler runs in a background thread which in this case, I guess, doesn't prevent the application main threat to terminate. BackgroundScheduler在后台线程中运行,我猜在这种情况下,它不会阻止应用程序主要威胁终止。

Try add at the end of your application: 尝试在应用程序末尾添加:

 import time

 print("Waiting to exit")
 while True:
    time.sleep(1)

... and then terminate your application with CTRL+C. ...,然后使用CTRL + C终止您的应用程序。

Threads are no magical way of making code run & managed by the OS. 线程并不是使代码由操作系统运行和管理的神奇方法。 They are local to your process only, and so if that process terminates or dies unexpectantly, so does your Thread. 它们仅在您的进程本地,因此,如果该进程意外终止或死亡,那么您的线程也是如此。

So the answer to your question is: don't use threads, write your program in a normal fashion so you can invoke it on your commandline, and then use a OS-based scheduler such as CRON to schedule it. 因此,您的问题的答案是:不要使用线程,以常规方式编写程序,以便可以在命令行上调用它,然后使用基于OS的调度程序(例如CRON)进行调度。

Alternatively, if your program needs to run continuously because it eg builds up caches that are expensive to re-compute every 5 minutes, use a process-observer such as supervisord to ensure even after a reboot or crash the program continues executing. 或者,如果您的程序需要持续运行,例如因为它建立了每5分钟重新计算昂贵的高速缓存,请使用监控程序之类的进程观察程序来确保即使在重新启动或崩溃后程序也可以继续执行。

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

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