I'm in a situation where I need to stop executing a thread when it matches a condition inside of current running/executing thread:
Here's the example what i mean:
- We have list of 5 numbers from 1 to 5.
- We queued them & processing them along with10 Number of Thread in Run_Calc function.
- in
Run_Calc
i want my thread to stop if it matches a condition of3
import threading,queue
q = queue.Queue()
numbers = [1,2,3,4,5]
for each_num in numbers:
q.put(each_num)
def Run_Calc(no):
print("NO IS : {}".format(no))
if no == 3:
print("YES I WANT TO EXIT HERE and DONT WANT TO EXECUTE 4,5")
def ProcessThreading():
while not q.empty():
get_number = q.get()
Run_Calc(get_number)
th_list = []
for i in range(10):
t = threading.Thread(target=ProcessThreading)
th_list.append(t)
t.start()
for th in th_list:
th.join()
- OUTPUT:
NO IS : 1
NO IS : 2
NO IS : 3
YES I WANT TO EXIT HERE and DONT WANT TO EXECUTE 4,5
NO IS : 4
NO IS : 5
- How do I achieve it?