1

我正在尝试设置一个 redis 队列和一个工作人员来使用我的烧瓶应用程序处理队列。我正在实现这个来处理发送电子邮件的任务。我有点困惑,因为堆栈跟踪似乎在说我的“APP_SETTINGS”环境变量实际上没有设置。

在启动应用程序、redis 或 worker 之前,我设置了 APP_SETTINGS:

export APP_SETTINGS="project.config.DevelopmentConfig"

但是,当一个项目被添加到队列中时,堆栈跟踪如下:

17:00:00 *** Listening on default...
17:00:59 default: project.email.sendMailInBG(<flask_mail.Message object at 0x7fc930e1c3d0>) (aacf9546-5558-4db8-9232-5f36c25d521b)
17:01:00 KeyError: 'APP_SETTINGS'
Traceback (most recent call last):
  File "/home/tony/pyp-launch/venv/local/lib/python2.7/site-packages/rq/worker.py", line 588, in perform_job
    rv = job.perform()
  File "/home/tony/pyp-launch/venv/local/lib/python2.7/site-packages/rq/job.py", line 498, in perform
    self._result = self.func(*self.args, **self.kwargs)
  File "/home/tony/pyp-launch/venv/local/lib/python2.7/site-packages/rq/job.py", line 206, in func
    return import_attribute(self.func_name)
  File "/home/tony/pyp-launch/venv/local/lib/python2.7/site-packages/rq/utils.py", line 150, in import_attribute
    module = importlib.import_module(module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/tony/pyp-launch/project/__init__.py", line 24, in <module>
    app.config.from_object(os.environ['APP_SETTINGS'])
  File "/home/tony/pyp-launch/venv/lib/python2.7/UserDict.py", line 40, in __getitem__
    raise KeyError(key)
KeyError: 'APP_SETTINGS'
Traceback (most recent call last):
  File "/home/tony/pyp-launch/venv/local/lib/python2.7/site-packages/rq/worker.py", line 588, in perform_job
    rv = job.perform()
  File "/home/tony/pyp-launch/venv/local/lib/python2.7/site-packages/rq/job.py", line 498, in perform
    self._result = self.func(*self.args, **self.kwargs)
  File "/home/tony/pyp-launch/venv/local/lib/python2.7/site-packages/rq/job.py", line 206, in func
    return import_attribute(self.func_name)
  File "/home/tony/pyp-launch/venv/local/lib/python2.7/site-packages/rq/utils.py", line 150, in import_attribute
    module = importlib.import_module(module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/tony/pyp-launch/project/__init__.py", line 24, in <module>
    app.config.from_object(os.environ['APP_SETTINGS'])
  File "/home/tony/pyp-launch/venv/lib/python2.7/UserDict.py", line 40, in __getitem__
    raise KeyError(key)
KeyError: 'APP_SETTINGS'
17:01:00 Moving job to u'failed' queue
17:01:00 
17:01:00 *** Listening on default...

电子邮件.py

from flask.ext.mail import Message

from project import app, mail
from redis import Redis
from rq import use_connection, Queue

q = Queue(connection=Redis())


def send_email(to, subject, template, emailable):

    if emailable==True:   
        msg = Message(
            subject,
            recipients=[to],
            html=template,
            sender=app.config['MAIL_DEFAULT_SENDER']
        )
        q.enqueue(sendMailInBG, msg)

    else:
        print("no email sent, emailable set to: " + str(emailable))

def sendMailInBG(msgContent):

    with app.test_request_context():
        mail.send(msgContent)

工人.py

import os

import redis
from rq import Worker, Queue, Connection

listen = ['default']

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

conn = redis.from_url(redis_url)

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

我真的很感激另一组对此的看法。我一生都无法弄清楚这里发生了什么。

4

1 回答 1

2

感谢@danidee的提示,我发现每个终端都需要定义环境变量。因此,APP_SETTINGS 是为实际应用程序定义的,而不是为工作人员定义的。

解决方案是在工作终端中设置 APP_SETTINGS。

于 2016-10-14T22:37:30.040 回答