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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With