RQ Flask Heroku工作者应用程序上下文

时间:2016-11-15 13:39:41

标签: python flask redis sqlalchemy worker

有app.py:

app = Flask(__name__)

with app.app_context():
    app.config.from_object(os.environ['APP_SETTINGS'])
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    app.debug = True

    db.app = app
    db.init_app(app)

if __name__ == '__main__':
    app.run()

和worker.py:

import os
import redis
from rq import Worker, Queue, Connection

listen = ['high', 'default', 'low']

redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')

conn = redis.from_url(redis_url)

if __name__ == '__main__':
    with Connection(conn):
        worker = Worker(map(Queue, listen))
        worker.work()

和其他包含

的文件
q = Queue(connection=conn)
q.enqueue(process_data, data)

其中process_data是使用db(sqlalchemy)

的函数

我应该如何将应用程序上下文传递给worker? 错误是:

File "/app/.heroku/python/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 153, in __init__:
self.app = app = db.get_app()
RuntimeError: application not registered on db instance and no application bound to current context

1 个答案:

答案 0 :(得分:0)

通过从init.py中的app.py(app.run()除外)和app.py导入应用程序中移动与app config相关的整个代码来解决并且像魅力一样工作