我在尝试运行代码时遇到导入错误。找不到调度程序模块。当我尝试使用backgroundScheduler()
绕过错误时,我发现它不支持类似cron的作业调度,即add_cron_job
。我哪里错了?
import ConfigParser
import pymysql # Allows us to connect to a database and issue queries
from time import sleep
from datetime import datetime
import threading, time
import json
from apscheduler.scheduler import Scheduler
import os
import logging
import smtplib # Import smtplib for the actual sending function
logging.basicConfig()
config = None
sched = Scheduler()
sched.start()
#----------------------------------------------------------------------------
# init_config_parser
# Initialize a config parser based on filename
#----------------------------------------------------------------------------
def init_config_parser():
global config
config = ConfigParser.SafeConfigParser()
config.readfp(open('settings.cfg'))
return config
# This is called when the runReportsScheduler.py file is first run.
# There is an infinite loop such that the script will never terminate.
# Every x number of seconds, the script reloads the latest
# notification schedules from the database. These notification schedules
# can be changed from the webapp in the configuration page.
# Then, it reschedules all of the email notifications accordingly.
# The scheduler calls the function email_send everytime it should be fired.
def email_send(emails):
From = 'web@weber.com'
To = emails
message = """Please ignore this email.
This is a test e-mail message.
"""
try :
smtpObj = smtplib.SMTP('smtphost.qualcomm.com')
smtpObj.sendmail(From, To.split(','), message) # the second parameter expects a list for multiple contacts
smtpObj.quit()
except SMTPException:
print "Error: unable to send email"
if __name__ == '__main__':
init_config_parser()
updateInterval = float(config.get("SCHEDULER",'updateInterval'))
thisURL = str(config.get("SCHEDULER",'rootURL'))
#refreshingFunction(50687,False)
i = 0
while True:
i += 1
print "This is the #"+str(i)+" time I am looping, on a "+str(updateInterval)+"s interval"
conn = pymysql.connect(host="10.52.244.877", user="wciadmin", passwd="admybutt", db="weekly_reports")
cur = conn.cursor(pymysql.cursors.DictCursor)
cur.execute("SELECT * FROM reminders_table ORDER BY id DESC")
currentJobs = sched.get_jobs()
row = cur.fetchone()
while row is not None:
# this is for each row in the DB
thisQuery = row['id']
# delete any current jobs with that query name
for job in currentJobs:
if int(job.name) == int(thisQuery):
sched.unschedule_job(job)
thisRuntimes = row['runtimes'].split(";")
if len(thisRuntimes) > 0 and row['runtimes'] != "":
for thisSchedule in thisRuntimes:
daysOfWeek = thisSchedule.split("-")[0]
times = thisSchedule.split("-")[1].split(",")
for time in times:
hour = time.split(":")[0]
if hour[0] == "0":
hour = hour[1]
min = time.split(":")[1]
sched.add_cron_job(email_send, day_of_week=daysOfWeek, hour=hour, minute=min, args=[row['emails']])
row = cur.fetchone()
conn.close()
sleep(updateInterval) #in seconds
答案 0 :(得分:2)
知道了。 Scheduler()已在apscheduler v3.0中弃用。我下载了2.1.2版,它解决了我的问题。 v2.1.2的链接如下: