views:

62

answers:

1

I've read through the documentation on threading for python and as I've pereceived it the following should hold true: You can access (read) any PoD or python specific object (such as an array) without causing failure in a multi-threaded program trying the same thing at the same time, but you can not change them and accept thread integrity.

My question is about classes. I have a server that is delegating database access to different threads, however I want them to be able to all access an instance of a class that handles response generation. However, I'm wondering if this class is thread-safe (I wish to avoid creating multiple instances), the thread does not change any instance variables (i.e. self.something = (something)) every function uses its own local variables (they do access class instance variables but does not change them), so to sum it up: my question is if many threads can use the same instance and call functions at the same time.

+1  A: 

Locals are thread-safe as they are not shared between threads. All constants (variables you never write to from any thread) are thread-safe. If that's all you have, then yes, that's fine. Ensure the class members you are talking about are really not written from any other thread.

Check there are no underlying shared resources that might not be thread-safe, eg. if each thread is using the database connection object that may cause trouble, unless that object is specifically documented as being thread-safe.

bobince
Okey. Thanks, I will take your advice and make the database connection individual to each thread.
Andreas