2016-10-14 4 views
1

이것을 실행하면 1 분 정도 기다렸다가 '켜짐'이 인쇄되고 2 분 정도 기다렸다가 '꺼짐'이 인쇄됩니다. 그 후에 apscheduler는 열매를 맺는 것처럼 보이고 빨리 2 개 사이에서 빨리 교체됩니다.왜 작동하지 않습니까? 이것은 apscheduler 버그입니까?

나는 apscheduler 버그에 넘어 지거나 왜 이런 일이 발생합니까?

from datetime import datetime, timedelta 
import time 
import os, signal, logging 
logging.basicConfig(level=logging.DEBUG) 

from apscheduler.schedulers.background import BackgroundScheduler 
scheduler = BackgroundScheduler() 


def turn_on(): 
    #Turn ON 
    print('##############################Lights on') 


def turn_off(): 
    #Turn off 
    print('#############################Lights off') 


def schedule(): 
    print('Lights will turn on at'.format(lights_on_time)) 
if __name__ == '__main__': 

    while True: 
     lights_on_time = (str(datetime.now() + timedelta(minutes=1))) 
     lights_off_time = (str(datetime.now() + timedelta(minutes=2))) 

     scheduler.add_job(turn_on, 'date', run_date=lights_on_time) 
     scheduler.add_job(turn_off, 'date', run_date=lights_off_time) 
     try: 
      scheduler.start() 
      signal.pause() 
     except: 
      pass 

    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) 

    try: 
     # This is here to simulate application activity (which keeps the main thread alive). 
     while True: 
      time.sleep(2) 
    except (KeyboardInterrupt, SystemExit): 
     # Not strictly necessary if daemonic mode is enabled but should be done if possible 
     scheduler.shutdown() 
+0

패키지에 익숙하지 않지만 절대 시간을 예약 했으므로 여기에 놀라움이 없습니다 – volcano

+0

나는이 스크립트에 여전히 문제가 있습니다. 그것은 하나의 사이클을 거치고 나서 계속 트리거 오류 메시지를 놓친다. 어떤 아이디어? TIA –

답변

2

이벤트로 스케줄러를 가득 채우고 있습니다. BackgroundScheduler를 사용하고 있습니다. 즉, scheduler.start()가 종료되고 이벤트가 발생할 때까지 기다리지 않습니다. 가장 간단한 해결 방법은 BackgroundScheduler (BlockingScheduler 사용)를 사용하지 않거나 루프에 sleep (180)을 넣는 것입니다.

+0

BlockingScheduler와 sleep을 사용하여 trtied했는데 여전히 작동하지 않습니다. –

+0

@ GabrielMoreno 왜 작동하지 않는지 자세히 설명해 주시겠습니까? –

+0

@ GabrielMoreno'job = scheduler.add_job (myfunc, 'interval', minutes = 2)'을 사용해 보셨습니까? docs [here] (http://apscheduler.readthedocs.io/en/latest/modules/schedulers/base.html#apscheduler.schedulers.base.BaseScheduler.add_job)를 참조하십시오. 이 작업은 한 번만 수행하면되고 while 루프는 사용할 필요가 없습니다. –

1

이 시도 :

from datetime import datetime, timedelta 
from apscheduler.schedulers.background import BackgroundScheduler 
import time 

scheduler = BackgroundScheduler() 


def turn_on(): 
    print('Turn on', datetime.now()) 


def turn_off(): 
    print('Turn off', datetime.now()) 


scheduler.start() 


while True: 
    scheduler.add_job(func=turn_on, trigger='date', next_run_time=datetime.now() + timedelta(minutes=1)) 
    scheduler.add_job(func=turn_off, trigger='date', next_run_time=datetime.now() + timedelta(minutes=2)) 
    time.sleep(180) 

한 번만 스케줄러를 시작해야합니다.

+0

귀하의 도움에 감사드립니다. –