简体   繁体   中英

Running apschduler in Python script as a daemon?

I have a job.py which has the following code.

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()
  • When I run this on the command prompt, it exits immediately:

INFO[2012-04-03 13:31:02,825]: Started thread pool with 0 core threads and 20 maximum threads INFO[2012-04-03 13:31:02,827]: Scheduler started INFO[2012-04-03 13:31:02,827]: Added job "run_job (trigger: cron[minute='2'], next run at: 2012-04-03 14:02:00)" to job store "default" INFO[2012-04-03 13:31:02,828]: Shutting down thread pool

  • How can I makde this run as a daemon?

Write your main() as below.

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

Additionally it shouldn't hurt to consider PEP 3143 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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