How to create Stopable thread in python:-
just copy and paste:)
Learning:-
#!/usr/bin/python2
import threading
import time
def myfunction(say):
print "my first thread is runnning"
while 1:
print say
time.sleep(1)
thread = threading.Thread(target=myfunction,args=("hello sir",))
print "Thread is alive: ",thread.isAlive()
t = threading.Event() #must after instance is created
thread.daemon = True #must after threading.event and before start the event
thread.start() #start the thread
time.sleep(1)
print "Check flag: ",t.isSet() #Check flag before stopping
time.sleep(1) #after 1 second delay stop thread
print "Attempting to stop thread"
t.set() #stop thread
print "Thread event: ",t.isSet() #Check thread is stopped or not
print "thread is alive :",thread.isAlive() #i dont know why it is returing true.
import threading
import time
def myfunction(say):
print "my first thread is runnning"
while 1:
print say
time.sleep(1)
thread = threading.Thread(target=myfunction,args=("hello sir",))
print "Thread is alive: ",thread.isAlive()
t = threading.Event() #must after instance is created
thread.daemon = True #must after threading.event and before start the event
thread.start() #start the thread
time.sleep(1)
print "Check flag: ",t.isSet() #Check flag before stopping
time.sleep(1) #after 1 second delay stop thread
print "Attempting to stop thread"
t.set() #stop thread
print "Thread event: ",t.isSet() #Check thread is stopped or not
print "thread is alive :",thread.isAlive() #i dont know why it is returing true.
Comments
Post a Comment