2016-10-15 6 views
0

답변을 위해 꽤 오랫동안 찾고 있었으므로 여기를 봤습니다! 그것은 나에게 오류, "잘못된 인덱스"sched.start()!이 APScheduler 코드가 작동하지 않는 이유는 무엇입니까?

import random 
import datetime 
from apscheduler.schedulers.blocking import BlockingScheduler 
import smtplib 
import email 


def randEmail(): 
    #Gets randLine 
    file_object = open("lyrics.txt", "r") 
    randLine = random.randint(1, 10) 
    for i, line in enumerate(file_object): 
     if i == randLine: 
      break 
    #line = randomly generated line 
    file_object.close() 

    #Email 
    emails = [ 'emails'] 


    server = smtplib.SMTP('smtp.gmail.com', 587) 
    server.starttls() 
    server.login('login', 'password') 
    server.sendmail('login',emails, line) 
    server.quit() 

    #Prints to notepad saying completed 
    Date = datetime.datetime.now() 


    with open("Server_Quit.txt", "r+") as ServerQuit: 
     ServerQuit.write("Server has quit at " + str(Date)) 
    ServerQuit.close() 


#Unsure whether working 
#sched.Scheduler() 
#sched.start() 
#sched.add_interval_job(randEmail, hours=24, start_date='2016-10-10 18:30') 

sched = BlockingScheduler() 
@sched.randEmail('cron', day_of_week='mon-fri', hour=18, minutes=30) 
sched.start() 

감사합니다. 나는이 일을 혼자서하기 위해 최선을 다 했었고, 다른 모든 문제를 스스로 해결했지만,이 일을 할 수는 없습니다. 또한이 작업을 내 PC에서 실행하고 매일 수행하기를 원한다면 시동 프로세스에 추가하면됩니까? 그리고 PC를 시작하면 스케줄러가 시작됩니까?

당신은 scheduled_job 장식과 기능을 장식하여 스케줄러에 작업을 추가 할 수 있습니다

답변

0

:

sched.add_job(randEmail, 'cron', day_of_week='mon-fri', hour=18, minute=30) 
sched.start() 

내가 어떤 이유를 볼 수 없습니다 :

from apscheduler.schedulers.blocking import BlockingScheduler 

sched = BlockingScheduler() 

# minute=30 not minutes=30 
@sched.scheduled_job('cron', day_of_week='mon-fri', hour=18, minute=30) 
def randEmail(): 
    #Gets randLine 
    with open("lyrics.txt", "r") as file_object: 
     randLine = random.randint(1, 10) 
     for i, line in enumerate(file_object): 
      if i == randLine: 
       break 
    #line = randomly generated line 

    #Email 
    emails = [ 'emails'] 

    server = smtplib.SMTP('smtp.gmail.com', 587) 
    server.starttls() 
    server.login('login', 'password') 
    server.sendmail('login', emails, line) 
    server.quit() 

    #Prints to notepad saying completed 
    Date = datetime.datetime.now() 

    # You don't have to close the file if you use the with statement 
    with open("Server_Quit.txt", "r+") as ServerQuit: 
     ServerQuit.write("Server has quit at " + str(Date)) 

sched.start() 

당신은 또한 add_job 방법을 사용할 수 있습니다 왜 그것이 시작 프로세스로 작동하지 않을 것입니다.

+0

해결할 수없는 "ImportError : No scheduler module"이 나타납니다 :/Apsheduler를 설치/제거하려고 시도했지만 작동하지 않습니다. – Mhyles

+0

이 가져 오기는 어디에서 발생합니까? 붙여 넣은 코드에는 표시되지 않습니다. –

+0

"apscheduler.schedulers.blocking import BlockingScheduler"가 맨 위에 표시됩니다. apscheduler를 사용하는 대신 Windows 작업 스케줄러를 사용하고 있습니다. 작동시키지 못했기 때문입니다. – Mhyles

관련 문제