Answer the question
In order to leave comments, you need to log in
Will I ever understand threads in Python?
We have the following test code:
import threading
import random
lock = threading.BoundedSemaphore(value=5)
def write(*args, **kwargs):
global lock
lock.acquire()
print "Thread name: %s. Result %-6f" % (args)
lock.release()
class App(threading.Thread):
def __init__(self, *args, **kwargs):
threading.Thread.__init__(self, *args, **kwargs)
def run(self):
for i in xrange(5000):
r = random.random()
i = i*i-789*96/2*r
write(self.name, i)
for i in xrange(5):
app = App(name="THREADING_%d" % (i))
app.start()
Answer the question
In order to leave comments, you need to log in
And here is what you need:
import threading
import random
lock = threading.RLock() # <<< one
def write(*args, **kwargs):
global lock
with lock: # <<< two
print "Thread name: %s. Result %-6f" % (args)
class App(threading.Thread):
def __init__(self, *args, **kwargs):
threading.Thread.__init__(self, *args, **kwargs)
self.setDaemon(False)
def run(self):
for i in xrange(5000):
r = random.random()
i = i*i-789*96/2*r
write(self.name, i)
for i in xrange(5):
app = App(name="THREADING_%d" % (i))
app.start()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question