Are Python instance variables thread-safe?

后端 未结 4 1554
北荒
北荒 2020-12-13 02:38

OK, check following codes first:

class DemoClass():

    def __init__(self):
        #### I really want to know if self.Counter is thread-safe. 
        self         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-13 03:06

    self.Counter is an instance variable, so each thread has a copy.

    If you declare the variable outside of __init__(), it will be a class variable. All instances of the class will share that instance.

提交回复
热议问题