Python Conditional “With” Lock Design

后端 未结 5 1994
孤独总比滥情好
孤独总比滥情好 2020-12-15 16:48

I am trying to do some shared locking using with statements

def someMethod(self, hasLock = False):
     with self.my_lock:
         self.somethingElse(hasLoc         


        
5条回答
  •  不知归路
    2020-12-15 17:40

    The with statement is a great way to implement locking, as locking is a perfect resource acquisition pattern. Though your current example won't work. You'll need an if statement around the with statement within somethingElse().

提交回复
热议问题