When to use locks in a threaded Web-server

Aahz aahz at pythoncraft.com
Mon Mar 25 17:17:18 EST 2002


In article <y83n8.15186$eJ6.285255 at news2.ulv.nextra.no>,
Thomas Weholt <thomas at weholt.org> wrote:
>
>I got a web-server project based on BaseHTTPServer using threads (
>SocketServer.ThreadingMixin ). What do I have to do to make it "safe",
>thread-wise? When do I have to aquire locks to set variables in a) the
>webserver itself or b) in variables in the given requesthandler ??

Generally speaking, you only need to lock when you've got an object
shared across multiple threads.  Doesn't look like you need any locking
in your example, but because Python objects are technically global and
Python provides a great deal of introspective capability, it can be
tricky to figure out what objects are shared.  Generally speaking, if
you use Queue.Queue() to transfer information between threads, you don't
need any locking.  See the OSCON 2001 slides and examples on my web page
for more details.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"We should forget about small efficiencies, about 97% of the time.
Premature optimization is the root of all evil."  --Knuth



More information about the Python-list mailing list