I am trying to create a work and a thread pool. Each thread from the pool tries to pick a work untill work is empty. Then all the threads join. When I run the program in idle, it seems that threads are not joining properly (Sometimes it executes properly). Even sometimes all threads do not participate in picking work properly.
Edit:But when I run the same in command shell, it works fine.
Below is the related code.
import threading
import time
threads = []
work = range(1000)
lock = threading.Lock()
def f():
global work
th = threading.currentThread()
name = str(th.getName())
while True:
lock.acquire()
try:
size = str(len(work))
## print "\n Inside "+ name +" "+ size + "\n"
if size!="0":
w = work.pop()
else:
break
finally:
lock.release()
print "\n"+ name +" "+ size + "\n"
start1 = time.time()
for i in range(10):
t = threading.Thread(target = f)
threads.append(t)
t.start()
for th in threads:
print "joining"
th.join()
end1 = time.time()
start2 = time.time()
work = range(1000)
for i in work:
print i
end2 = time.time()
print end1-start1, end2-start2