Static lock in Python?
Posted
by roddik
on Stack Overflow
See other posts from Stack Overflow
or by roddik
Published on 2010-03-08T22:49:18Z
Indexed on
2010/03/08
22:51 UTC
Read the original article
Hit count: 285
Hello. I've got the following code:
import time
import threading
class BaseWrapper: #static class
lock = threading.Lock()
@staticmethod
def synchronized_def():
BaseWrapper.lock.acquire()
time.sleep(5)
BaseWrapper.lock.release()
def test():
print time.ctime()
if __name__ is '__main__':
for i in xrange(10):
threading.Thread(target = test).start()
I want to have a method synchronized using static lock. However the above code prints the same time ten times, so it isn't really locking. How can I fix it? TIA
© Stack Overflow or respective owner