在Python脚本中运行apschduler作为守护进程?

时间:2012-04-03 20:32:05

标签: python

我有job.py,其中包含以下代码。

import datetime
import logging
import sys
import os

from apscheduler.scheduler import Scheduler
from src.extractors.pExtractor import somejob

def run_job():
    start = datetime.datetime.now()
    logging.debug('Proposal extraction job starting')
    somejob.main()
    end = datetime.datetime.now()
    duration = end - start
    logging.debug('job completed , took ' + str(duration.seconds) + ' seconds')

def main():
    logging.basicConfig(filename='/tmp/pExtractor.log', level=logging.DEBUG,format='%(levelname)s[%(asctime)s]: %(message)s')
    sched = Scheduler()
    sched.start()
    sched.add_interval_job(run_job, minutes=2)

if __name__ == '__main__':
    main()
  • 当我在命令提示符下运行它时,它会立即退出:
  

INFO [2012-04-03 13:31:02,825]:启动了0核心线程的线程池   和20个最大线程信息[2012-04-03 13:31:02,827]:调度程序   启动INFO [2012-04-03 13:31:02,827]:添加了作业“run_job(触发器:   cron [分钟='2'],下次运行于:2012-04-03 14:02:00)“到工作店   “default”INFO [2012-04-03 13:31:02,828]:关闭线程池

  • 如何将此次运行视为守护程序?

1 个答案:

答案 0 :(得分:0)

写下您的main(),如下所示。

def main():
    [... your_code_as_in_your_question ...]
    while (True):
        pass

此外,考虑PEP 3143也不应该受到伤害。