擅长:python、mysql、java
<p>我不喜欢自由变量。我可能会将锁作为decorator的显式变量,如下所示:</p>
<pre><code>def mlock(Coloring):
def mlock_decorator(orig):
def inner(*args, **kwargs):
Coloring.lock.acquire()
try:
ret = orig(*args, **kwargs)
return ret
finally:
Coloring.lock.release()
return inner
return mlock_decorator
</code></pre>
<p>好吧,既然<code>Coloring</code>是一个类变量,并且如果您希望它对该类非常特定,那么显式地访问它:</p>
^{pr2}$
<p>或者,至少声明锁是全局的</p>
<pre><code>def mlock(orig):
global Coloring
def inner(*args, **kwargs):
Coloring.lock.acquire()
try:
ret = orig(*args, **kwargs)
return ret
finally:
Coloring.lock.release()
return inner
</code></pre>