我正在尝试添加一个SQLAlchemyJobStore
工作商店(并使其成为default
工作商店)并在其上存储一些工作。我正在运行mysql
它有一个名为jobstore
.
我有以下程序试图打开一个SQLAlchemyJobStore
作业存储到mysql
正在运行的数据库:
# sqlalchemy.py
from sqlalchemy import *
from apscheduler.jobstores.sqlalchemy_store import SQLAlchemyJobStore
from apscheduler.scheduler import Scheduler
from datetime import datetime, timedelta
import time
def alarm(time):
print('Alarm! This alarm was scheduled at %s.' % time)
_aps_config = {'standalone': 'True'}
_dbURL = 'mysql://root:<root-password>@localhost/jobstore'
if __name__ == '__main__':
scheduler = Scheduler(_aps_config)
scheduler.add_jobstore(SQLAlchemyJobStore(url=_dbURL), 'default')
alarm_time = datetime.now() + timedelta(seconds=10)
scheduler.add_date_job(alarm, alarm_time, name='alarm1', args=[datetime.now()])
print 'alarms added: ', alarm_time
alarm_time = datetime.now() + timedelta(seconds=15)
scheduler.add_date_job(alarm, alarm_time, name='alarm2', args=[datetime.now()])
print 'alarms added: ', alarm_time
alarm_time = datetime.now() + timedelta(seconds=20)
scheduler.add_date_job(alarm, alarm_time, name='alarm3', args=[datetime.now()])
print 'alarms added: ', alarm_time
try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
scheduler.shutdown()
pass
尝试运行上述代码时,我看到以下内容:
NameError: global name 'create_engine' is not defined
$ python sqlalchemy.py
Traceback (most recent call last):
File "sqlalchemy.py", line 19, in <module>
scheduler.add_jobstore(SQLAlchemyJobStore(url=_dbURL), 'default')
File "/usr/lib/python2.7/site-packages/APScheduler-2.1.0-py2.7.egg/apscheduler/jobstores/sqlalchemy_store.py", line 29, in __init__
self.engine = create_engine(url)
NameError: global name 'create_engine' is not defined
$
我明白"/usr/lib/python2.7/site-packages/APScheduler-2.1.0-py2.7.egg/apscheduler/jobstores/sqlalchemy_store.py"
了,它__init__
正在尝试create_engine
并且它失败了。
20 class SQLAlchemyJobStore(JobStore):
21 def __init__(self, url=None, engine=None, tablename='apscheduler_jobs',
22 metadata=None, pickle_protocol=pickle.HIGHEST_PROTOCOL):
23 self.jobs = []
24 self.pickle_protocol = pickle_protocol
25
26 if engine:
27 self.engine = engine
28 elif url:
29 self.engine = create_engine(url)
这里出了什么问题?!换句话说,我如何创建一个SQLAlchemyJobStore
usingAPScheduler
并成功地将作业存储在它们上面?任何示例/代码片段都会有很大帮助!