我正在嘗試添加SQLAlchemyJobStore
作業存儲(並將其作爲default
作業存儲)並在其上存儲一些作業。我正在運行mysql
,它有一個名爲jobstore
的數據庫。NameError:未定義全局名稱'create_engine'[嘗試創建SQLAlchemyJobStore時]
我有試圖打開一個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)
這裏怎麼回事?換句話說,我如何使用APScheduler
創建SQLAlchemyJobStore
併成功將作業存儲在它們上?任何示例/代碼片段將是一個很好的幫助!
你是對的;在APScheduler谷歌小組上討論過同樣的問題,一位來自社區的紳士也強調了這一點!謝謝! +1 –
謝謝你節省了一天 –