multithreading

Aahz aahz at pythoncraft.com
Fri Feb 21 21:36:33 EST 2003


In article <878dc260.0302210733.58c4f521 at posting.google.com>,
Renzo Tomaselli <renzo.tomaselli at tecnotp.it> wrote:
>
>assume I have a Python obj instance in a web application after
>Apache+modPython.
>The point is: can I assume that accessing local variables within a
>method of such instance is thread-safe, e.g. syncronized by the
>interpreter against concurrent invokations by multiple threads (e.g.
>multiple web clients) ?
>In a C++ environment, such automatic variables would be on the stack,
>e.g. they would exist per-thread. But in Python they are in the heap,
>so I dont't know about concurrency.

Because Python is so introspective, nothing is completely safe.
However, if your scope is inside a method, any objects created within
that method call are essentially safe.  What may not be safe is
accessing objects passed in to the method (if they're mutable), and it
may not be safe to access "self" if multiple threads contain references
to that object.

In other words, while the objects are created on the heap, the use of
local names as the only bindings for those objects makes it safe.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Register for PyCon now!  http://www.python.org/pycon/reg.html




More information about the Python-list mailing list