python - Are classmethods thread safe? -


i working on class operates in multithreaded environment, , looks (with excess noise removed):

class b:      @classmethod     def apply(cls, item):         cls.do_thing(item)      @classmethod     def do_thing(cls, item)         'do item'      def run(self):         pool = multiprocessing.pool()         list_of_items in self.data_groups:             pool.map(list_of_items, self.apply) 

my concern 2 threads might call apply or do_thing @ same time, or subclass might try stupid cls in 1 of these functions. use staticmethod instead of classmethod, calling do_thing become lot more complicated, if subclass reimplements 1 of these not other. question this: above class thread-safe, or there potential problem using classmethods that?

whether method thread safe or not depends on method does.

working local variables thread safe. when change same non local variable different threads, becomes unsafe.

‘do item’ seems modify given object, independent other object in list, should thread safe.

if same object in list several times, may have think making object thread safe. can done using with self.object_scope_lock: in every method modifies object.

anyway, doing here using processes instead of threads. in case objects pickled , send through pipe other process, modified , send back. in contrast threads processes not share memory. don’t think using lock in class-method have effect.

http://docs.python.org/3/library/threading.html?highlight=threading#module-threading


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -