I am working on multithreading in python i could not achieve to stop individual threads. The code is below. How can i achieve this? Thanks...
from threading import Thread
import threading
import time
class MyThread(threading.Thread):
def stop(self):
self.stopped = True
print ("working")
stopped=True
def func(argument):
t = threading.current_thread()
while not t.stopped:
print(argument)
time.sleep(0.5)
a = MyThread(target=func,args=("1",))
b = MyThread(target=func,args=("2",))
c = MyThread(target=func,args=("3",))
d = MyThread(target=func,args=("4",))
a.daemon = True
b.daemon = True
c.daemon = True
d.daemon = True
a.start()
b.start()
c.start()
d.start()
time.sleep(3)
b.stop()
c.stop()
d.stop()
After execution of this code, a thread must be alive and still running the function but all of the threads stop.