3

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 /

N3TC4T
  • 197
  • 1
  • 2
  • 14
  • You asked similar question before, and have accepted answer: http://stackoverflow.com/questions/26068819/kill-all-pool-workers-in-python-multiprocess. What has been changed? – Tsyvarev Jul 13 '15 at 09:30
  • @Tsyvarev , There is many different between multiprocessing module and threading module in python . – N3TC4T Jul 13 '15 at 10:10
  • Oh, yes, I didn't pay attention that old question told about processes. As for threads, you cannot directly kill other thread, you should cooperate with it. See, e.g. that question: http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python. – Tsyvarev Jul 13 '15 at 10:43

1 Answers1

0

I tried to find a solution for my problem and this is my code : hope someone make it better

import Queue , threading 

q = Queue.Queue(maxsize=0)
threads = []
max_thread = 10
exitFlag = False
users_id = []
collected_users = []

queueLock = threading.Lock()

for x in range(1,50):
    users_id.append(x)

def my_function():
    if not q.empty():
       ids = q.get()
       print (ids)
       if (ids < 5000 ):
            collected_users.append(ids)
       if(len(collected_users) >= 10) :
            print ("collected")
            exitFlag = True
            q.queue.clear()
            q.task_done()
       else:
            q.task_done()
            return
try :
    for ids in users_id:
        q.put(ids)
    while not q.empty():
        queueLock.acquire()
        for workers in range(max_thread):
            t = threading.Thread(target=my_function)
            t.setDaemon(True)
            t.start()
            threads.append(t)
        for t in threads:
            t.join()
        queueLock.release()
        if (exitFlag == True):
            break

   print ("All jobs finished")
except Exception as errtxt:
   print errtxt
N3TC4T
  • 197
  • 1
  • 2
  • 14