使用锁机制和变量作为全局变量(在类范围之外)或类变量(在问题中声明)有什么区别?如:
class httpHandler(BaseHTTPRequestHandler):
success = 0
fails = 0
statsLock = threading.Lock()
def do_POST(self):
...
httpHandler.statsLock.acquire()
httpHandler.success += 1
self.statsLock.release()
return
或者:
success = 0
fails = 0
statsLock = threading.Lock()
class httpHandler(BaseHTTPRequestHandler):
def do_POST(self):
...
statsLock.acquire()
success += 1
statsLock.release()
return