How can I kill remained threads in the middle of jobs in python ? an example code :
q = Queue.Queue(maxsize=0)
threads = []
max_thread = 10
locker = threading.Semaphore(value=max_thread)
try :
for user in users_list:
t = threading.Thread(target=my_function, args=(user,))
t.setDaemon(True)
threads.append(t)
t.start()
for thread in threads:
q.put(thread)
q.join()
except Exception as errtxt:
print errtxt
the function for example is used in thread pools ,in this function i wanna collect just 30 user with payment more than 5000
def my_function(user):
locker.acquire()
if (user.payment > 5000 ):
collected_users.append(user.id)
if(len(collected_users) >= 30 :
return # <- here i wanna kill other remained threads
q.task_done()
locker.release()
I think i should set a controller in queue /