Directly calling threaded class instance methods and attributes

Diez B. Roggisch deetsNOSPAM at web.de
Thu Oct 28 07:40:16 EDT 2004


> My question is simply, can anyone see any issues with calling methods
> and/or attributes of a threaded class instance like this?  It looks ok
> to me but, as the docs never seem to mention using threads like this,
> I'm wondering if I've missed something important.  If it helps, I
> know the basic considerations of threading, such as locking, exception
> handling and so on; I only really need advice on whether there could
> be issues with directly calling class instance attributes of a
> running thread.
> 
> If anyone could let me know either "Yeah, that's fine" or "NO!!! That
> can break <foo> / cause a deadlock in <bar> / etc!!!" I'd be much
> obliged.

Python is very therad-friendly in a way that you don't get SIGSEGVs for
doing this - that means that at least the internal data-structures are
alwasys consistent. As a rule of thumb one can say that every expression is
atomic, and thus leaves the interpreter in a consistent state. But beware!
This is more than one expression:

a = b + c * d

It could be rewritten like this:

h = c* d
a = b + h

which makes it at least two - maybe there are even more. But

l.append(10)

on a list will at least be atomic when the actual appending occurs - thus
its perfectly ok to have 10 workerthreads appending to one list, and one
consumer thread pop'ing values from it.

So your code is perfectly legal, and as long as you are aware that having
more complex operations on objects undergoing can be interrupted at any
time, maybe leaving data inconsistent _from and applications POV_ - then
you'll need explicid sync'ing, by locks, queues or whatever...

-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list