python-daemon 1.6 작업 목록을 관리하는 데 APScheduler를 사용하는 데 문제가 있습니다.python crontab 대안 - APScheduler & python-daemon
:작업 (Ctrl + C를 누르면까지)
from apscheduler.scheduler import Scheduler
import logging
import signal
def job_function():
print "Hello World"
def init_schedule():
logging.basicConfig(level=logging.DEBUG)
sched = Scheduler()
# Start the scheduler
sched.start()
return sched
def schedule_job(sched, function, periodicity, start_time):
sched.add_interval_job(job_function, seconds=periodicity, start_date=start_time)
if __name__ == "__main__":
sched = init_schedule()
schedule_job(sched, job_function, 120, '2011-10-06 12:30:09')
schedule_job(sched, job_function, 120, '2011-10-06 12:31:03')
# APSScheduler.Scheduler only works until the main thread exits
signal.pause()
# Or
#time.sleep(300)
샘플 출력 -
은 (초 해상도 스케줄러는 특정 선택한 시간에 주기적으로 실행해야합니다)INFO : apscheduler.threadpool : 코어 스레드 0 개와 최대 스레드 20 개가 포함 된 스레드 풀이 시작됨 INFO : apscheduler.scheduler : Sched uler가 시작했습니다. DEBUG : apscheduler.scheduler : 실행할 작업을 찾고 있습니다. DEBUG : apscheduler.scheduler : 작업이 없습니다. 작업이 추가 될 때까지 기다림 INFO : apscheduler.scheduler : "job_function (trigger : interval [0:00:30], 다음 실행 시간 : 2011-10-06 18:30:39)"작업 저장소로 "default" " 정보 : apscheduler.scheduler :"job_function (trigger : interval [0:00:30], 다음 실행 : 2011-10-06 18:30:33) "작업을"default "로 추가 DEBUG : apscheduler .scheduler : 일자리를 찾고 DEBUG를 실행합니다 : apscheduler.scheduler 다음 : 웨이크은
파이썬 데몬으로, 출력은 비어 2011-10-06 18시 30분 33초 (10.441128 초)에 기인한다 . DaemonContext가 프로세스를 올바르게 생성하지 않는 이유는 무엇입니까?
편집 - 작업
파이썬 데몬 소스를 읽은 후, 나는 DaemonContext에 표준 출력과 표준 에러를 추가하고 마지막에 무슨 일이 있었는지 알 수 있었다했습니다. job_function()
에서
def job_function():
print "Hello World"
print >> test_log, "Hello World"
def init_schedule():
logging.basicConfig(level=logging.DEBUG)
sched = Scheduler()
sched.start()
return sched
def schedule_job(sched, function, periodicity, start_time):
sched.add_interval_job(job_function, seconds=periodicity, start_date=start_time)
if __name__ == "__main__":
test_log = open('daemon.log', 'w')
daemon.DaemonContext.files_preserve = [test_log]
try:
with daemon.DaemonContext():
from datetime import datetime
from apscheduler.scheduler import Scheduler
import signal
logging.basicConfig(level=logging.DEBUG)
sched = init_schedule()
schedule_job(sched, job_function, 120, '2011-10-06 12:30:09')
schedule_job(sched, job_function, 120, '2011-10-06 12:31:03')
signal.pause()
except Exception, e:
print e
파이썬에서 간단한 cron 대체품이 있습니다. [here] (https://github.com/nibrahim/openlibrary/blob/master/openlibrary/core/minicron.py)에서 사용할 수 있으며 진입 점 스크립트는 [여기] (https://github.com/nibrahim/)입니다. openlibrary/blob/master/scripts/minicron.py)를 사용하면됩니다. 아직 앱을 별도의 앱으로 리팩터링 할 시간을 찾지 못했습니다. –
감사합니다 Noufal. 나는 그것을 시도 할 것이다. –
나는 당신의 의견을 환영합니다. 나는 그것을 분리 된 패키지로 만들어야한다. –