Thread-safe or not?

2 views
Skip to first unread message

Goodrone

unread,
May 13, 2008, 2:52:01 PM5/13/08
to Django users
What is the most up-to-date information about multithread safety in
Django? Is it a good idea to spawn several threads and use models from
there?

Rajesh Dhawan

unread,
May 13, 2008, 5:13:24 PM5/13/08
to Django users
Several threads within one request/view?

TP

unread,
May 13, 2008, 5:20:36 PM5/13/08
to Django users
How about specifically whether mod_wsgi can be used to run many
threads serving web requests for Django?

rcs_comp

unread,
May 13, 2008, 11:16:13 PM5/13/08
to Django users
On May 13, 5:20 pm, TP <thomaspinckn...@gmail.com> wrote:
> How about specifically whether mod_wsgi can be used to run many
> threads serving web requests for Django?

My understanding is that you can only run one instance of Django per
Application Group/Pool. You can read here for more info:

http://groups.google.com/group/django-developers/browse_thread/thread/ebcbb544d42849c6

Graham Dumpleton

unread,
May 14, 2008, 2:43:52 AM5/14/08
to Django users
> http://groups.google.com/group/django-developers/browse_thread/thread...

That issue is nothing to do with thread safety. It relates to running
multiple Django instances within same Python interpreter.

Graham

Goodrone

unread,
May 14, 2008, 4:50:45 AM5/14/08
to Django users
Well, I start several threads in some views, and in some other views I
notify them to do something.

We can consider a good example: an IM bot. Can it live in a separate
thread within a Django instance? Can the bot use the ORM from its
thread?

Pigletto

unread,
May 14, 2008, 7:57:32 AM5/14/08
to Django users
> > > What is the most up-to-date information about multithread safety in
> > > Django? Is it a good idea to spawn several threads and use models from
> > > there?
You may take a look there:
http://code.djangoproject.com/wiki/DjangoSpecifications/Core/Threading

Recently I tried Apache in MPM worker mode with multiple threads and
get some "strange" errors (especially in django template system).
I think these errors were related to threadsafety, especially that now
I'm running my applications with mod_wsgi in daemon mode with only 1
thread (no multithreading) and errors are gone.

Goodrone

unread,
May 14, 2008, 8:10:46 AM5/14/08
to Django users
On May 14, 3:57 pm, Pigletto <pigle...@gmail.com> wrote:
> You may take a look there:http://code.djangoproject.com/wiki/DjangoSpecifications/Core/Threading
Yeah, I've read that paper -- it looks like an investigation, but it
seems to have no conclusion which could help me to make a right design
decision.

> Recently I tried Apache in MPM worker mode with multiple threads and
> get some "strange" errors (especially in django template system).
> I think these errors were related to threadsafety, especially that now
> I'm running my applications with mod_wsgi in daemon mode with only 1
> thread (no multithreading) and errors are gone.

I don't use any Django templating (Mako instead) -- should I still
avoid creating multiple threads in my Django instance?

rcs_comp

unread,
May 14, 2008, 12:09:01 PM5/14/08
to Django users


On May 14, 2:43 am, Graham Dumpleton <Graham.Dumple...@gmail.com>
wrote:
Sorry, apparently I misunderstood. So, when he said:

How about specifically whether mod_wsgi can be used to run many
threads serving web requests for Django?

he meant many threads serving the same Django project instance? Would
the goal here to be increased performance without the overhead of
having multiple processes consuming resources?

Thanks.

Pigletto

unread,
May 14, 2008, 3:17:39 PM5/14/08
to Django users
> Yeah, I've read that paper -- it looks like an investigation, but it
> seems to have no conclusion which could help me to make a right design
> decision.
(...)
> I don't use any Django templating (Mako instead) -- should I still
> avoid creating multiple threads in my Django instance?
I think that nobody can tell you that this will be thread safe or not.
You have to try it yourself. In my case I had some strange errors that
don't appear when running applications with only one thread per
process. I don't know exactly why these errors appear- I mean this
might be related to django itself or to some external applications I
use (eg. django multilingual).

--
Maciej Wisniowski

Alex Morega

unread,
May 14, 2008, 4:26:04 PM5/14/08
to django...@googlegroups.com

On May 14, 2008, at 22:17 , Pigletto wrote:

>
>> Yeah, I've read that paper -- it looks like an investigation, but it
>> seems to have no conclusion which could help me to make a right
>> design
>> decision.
> (...)
>> I don't use any Django templating (Mako instead) -- should I still
>> avoid creating multiple threads in my Django instance?
> I think that nobody can tell you that this will be thread safe or not.

No, the question is valid. It is a question of wether the code in
Django is written with thread-safety in mind or not. If it's not, then
it's not thread-safe (because it makes use of globals for config,
caching, etc). Experimenting is not a good way to settle the issue,
because it does not guarantee safety (for example, some problems may
only come up when moving the code to production, or when scaling to
more visitors).

Reply all
Reply to author
Forward
0 new messages