miguelgrinberg/flask-celery-example

kombu.exceptions.EncodeError EncodeError: <flask_mail.Message object at 0x7fe0b576f510> is not JSON serializable

kenzhaoyihui opened this issue · 3 comments

When I use your code fot testing , it raised the error, because the celery receive the json data, not the Message object.

Traceback (most recent call last):
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/flask/app.py", line 1997, in call
return self.wsgi_app(environ, start_response)
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functionsrule.endpoint
File "/home/yzhao_sherry/work/venv/pypro/flask-celery-example/app.py", line 285, in index
send_async_email.apply_async(args=[msg], countdown=60)
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/celery/app/task.py", line 536, in apply_async
**options
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/celery/app/base.py", line 737, in send_task
amqp.send_task_message(P, name, message, **options)
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/celery/app/amqp.py", line 554, in send_task_message
**properties
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/kombu/messaging.py", line 169, in publish
compression, headers)
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/kombu/messaging.py", line 252, in _prepare
body) = dumps(body, serializer=serializer)
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/kombu/serialization.py", line 221, in dumps
payload = encoder(data)
File "/usr/lib64/python2.7/contextlib.py", line 35, in exit
self.gen.throw(type, value, traceback)
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/kombu/serialization.py", line 54, in _reraise_errors
reraise(wrapper, wrapper(exc), sys.exc_info()[2])
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/kombu/serialization.py", line 50, in _reraise_errors
yield
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/kombu/serialization.py", line 221, in dumps
payload = encoder(data)
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/kombu/utils/json.py", line 72, in dumps
**dict(default_kwargs, **kwargs))
File "/usr/lib64/python2.7/json/init.py", line 251, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib64/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/kombu/utils/json.py", line 62, in default
return super(JSONEncoder, self).default(o)
File "/usr/lib64/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
EncodeError: <flask_mail.Message object at 0x7fe0b576f510> is not JSON serializable

Then I create the Message object in the celery task, it didn't send the email, but the flashed information was displayed ! Could you help me do some check?

@celery.task
 def send_async_email(to):
     """Background task to send an email with Flask-Mail."""
     subject = "Hello from me"
     sender = app.config['MAIL_DEFAULT_SENDER']
     recipients = [to]
     msg = Message(subject, sender=sender, recipients=recipients)
     msg.body = 'This is a test email sent from a background Celery task.'
     with app.app_context():
         mail.send(msg)

Check the celery console, raise the error:
[2018-03-15 17:51:29,454: INFO/MainProcess] Received task: app.send_async_email[bb0b1a34-4428-4723-b8df-5f1ba5b1b6ab]
[2018-03-15 17:51:31,554: ERROR/ForkPoolWorker-1] Task app.send_async_email[bb0b1a34-4428-4723-b8df-5f1ba5b1b6ab] raised unexpected: AttributeError("'unicode' object has no attribute 'send'",)
Traceback (most recent call last):
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/celery/app/trace.py", line 374, in trace_task
R = retval = fun(*args, **kwargs)
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/celery/app/trace.py", line 629, in protected_call
return self.run(*args, **kwargs)
File "/home/yzhao_sherry/work/venv/pypro/flask-celery-example/app.py", line 107, in send_async_email
}
File "/home/yzhao_sherry/work/venv/lib/python2.7/site-packages/flask_mail.py", line 492, in send
message.send(connection)
AttributeError: 'unicode' object has no attribute 'send'

This issue will be automatically closed due to being inactive for more than six months. Seeing that I haven't responded to your last comment, it is quite possible that I have dropped the ball on this issue and I apologize about that. If that is the case, do not take the closing of the issue personally as it is an automated process doing it, just reopen it and I'll get back to you.