Closed mistalaba closed 9 years ago
Good morning.
I think the connection object is the problem. django.core.mail.backends.smtp.EmailBackend
uses thread locking and that makes it impossible to be pickled (serialized).That's just a pickle limitation. In this case you can probably just omit the connection to the EmailMultiAlternativesWithEncoding
call and let the worker create a fresh one or add a default to your function:
def send_single_correspondence(subject, text_content, html_content, from_email, recipient, connection=get_connection()):
It's also better to use a dotted string path to your mail function e.g. 'fysiografen.mail_utils.send_single_correspondence', this will make sure it gets freshly imported when called by the worker.
In this case you can probably just omit the connection to the EmailMultiAlternativesWithEncoding call and let the worker create a fresh one or add a default to your function
Since I'm most likely won't change the connection (sending these through Mandrill), I put the get_connection()
in the send_single_correspondence
and omitted it completely in the call later.
This worked like a charm!
It's also better to use a dotted string path to your mail function e.g. 'fysiografen.mail_utils.send_single_correspondence', this will make sure it gets freshly imported when called by the worker.
Thank you for the tip, I didn't know this!
Thanks so much for your work, django-q is so much nicer to work with than celery!
Hi Legends, if you're searching "Django Q pickle error" and end of here, this thread helped me fix my error. For others, I found out a few things the hard way:
async
arguments) complex objects. I was pass a complex object in so I could run self.method() in the queue. I changed to sending the object id (in the database) and look it up in a normal (static) function.
I am trying to send emails using django-q, but am getting a "TypeError: can't pickle lock objects" error trying the below. It works without the async. Sorry if I'm stupid, but I can't get this to work :/
Method for sending single email
and the async call
Probably not important, this is just a class that's overridden to handle attachents better:
The variables sent to
send_single_correspondence
are: subject: u'Async sending 12:23' text_content: 'Message body' html_content:u'<p>Message body</p>'
from_email: u'fromemail@example.com' recipient: u'toemail@example.com' connection:<django.core.mail.backends.smtp.EmailBackend object at 0xb49aa0c>
Traceback:
And here's the faulting line:
Perhaps I'm going about this the wrong way? It seems that I'm missing some information on the restrictions for django-q...?
Thanks for looking into this!