Thread-safeness of templates

30 views
Skip to first unread message

Francis Kung

unread,
Oct 4, 2011, 12:45:31 PM10/4/11
to Django users
Hi,

My understanding is that Django is (or attempts to be) thread-safe,
and I'm hoping someone can help me track down this problem:

Part of my app uses django templates to create the content of an
email, which it then sends out. The code is something like,

email = 'a...@def.com'
context = {'email': email}
body = render_to_string('template.html', context)
send_email(to=email, body=body)

where template.html is something like,
Hello, this email is being sent to {{ email }}.

And it's been working fine for over a year... but recently it was
brought to my attention that an email recipient didn't match the email
body - a few people had received emails that made no sense (the email
addresses in the "to" line and the body were different).

I did some searching, and there were a few concurrent requests at the
time; the mix-ups were with emails generated & sent within
milliseconds of each other, and I'm strongly suspect they were in
different requests/threads (based on the email addresses involved).

Is it possible (or known) that render_to_string isn't threadsafe
(django 1.2 / python 2.6)? Or is the likely cause somewhere else?
Just trying to figure out the best place to start looking.

Thanks in advance!
Francis

Martin J. Laubach

unread,
Oct 4, 2011, 7:23:38 PM10/4/11
to django...@googlegroups.com
  Are you sure your context is thread-safe, ie. it's rebuilt from scratch every time you render an email and not re-used, stored in a global variable, class variable, whatever? Your problem description very much sounds like someone is fiddling with the context while the template is rendering.

  A popular way to generate such a problem would be to use a literal dict as default parameter, something like:

    def my_render_email(recipient, context={}):
        context['email'] = recipient
        body = render_to_string('template.html', context
        ...

  That is absolutely positively guaranteed to blow up in your face when you least expect it.

        mjl


Brian Bouterse

unread,
Oct 5, 2011, 10:08:28 AM10/5/11
to django...@googlegroups.com
To add to your example.  Default arguments are parsed once by the interpreter, not each time the program is run.  Most folks don't desire side effects like this.  One way to avoid it is to only use immutable types in python as keyword arguments (True, False, None, numbers, strings, tuples).

Brian



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/mW012mKt7pwJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Brian Bouterse
ITng Services
Reply all
Reply to author
Forward
0 new messages