6

我正在尝试添加一个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)

这里出了什么问题?!换句话说,我如何创建一个SQLAlchemyJobStoreusingAPScheduler并成功地将作业存储在它们上面?任何示例/代码片段都会有很大帮助!

4

1 回答 1

4

您的代码似乎没有问题。我尝试运行它,它成功完成并显示以下输出:

python jobstore.py
alarms added:  2013-02-07 10:31:10.234000
alarms added:  2013-02-07 10:31:15.240000
alarms added:  2013-02-07 10:31:20.240000

我所做的唯一更改是更新_dbURL = 'sqlite:///:memory:'为使用 sqlite 引擎。

请检查您是否安装了 sqlalchemy,并且可以通过您的脚本在 PYTHONPATH 中找到它。在 python 控制台中运行以下代码,或者最好将其添加到脚本的开头并检查输出。

import sqlalchemy
print sqlalchemy.__version__ 

更新 我重新阅读了您的帖子,并意识到我的测试代码还有一个不同之处-我创建了另一个名称的文件:jobstore.py

我试图将文件重命名为 sqlalchemy.py 并得到同样的异常:

Traceback (most recent call last):
  File "C:/stackoverflow/so/sqlalchemy.py", line 22, in <module>
    scheduler.add_jobstore(SQLAlchemyJobStore(url=_dbURL), 'default')
  File "C:\Progs\Python27\lib\site-packages\apscheduler\jobstores\sqlalchemy_store.py", line 29, in __init__
    self.engine = create_engine(url)
NameError: global name 'create_engine' is not defined

Process finished with exit code 1

基本上问题是您的python脚本名称与sqlalchemy模块名称具有相同的名称,因此python首先加载您的脚本并且无法访问sqlalchemy代码。

尝试将脚本名称重命名为其他名称sqlalchemy.py- 如果您安装了 sqlalchemy 模块,这将有所帮助。

于 2013-02-07T15:37:25.833 回答