Skip to main content

Posts

Showing posts from November, 2016

Python stoppable thread

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

How to make stoppable thread in python

Python multi threading: stoppable thread:     many of you heard about threading in python is very bad. its not easy to stop it but there are several method to do that thing happen .     so lets get started.     create file name called <filename>.py then paste this code in your text editor. import threading import time ##writer = Robokishan ##stopable thread class ThreadingExample(object):     """ Threading example class     The run() method will be started and it will run in the background     until the application exits.     """     #NOTE: STOPING THREAD IS BAD METHOD     def __init__(self, interval=1):         """ Constructor         :type interval: int         :param interval: Check interval, in seconds         """         self.interval = interval         thread = threading.Thread(target=self.run, args=())         self._stop = threading.Event()         thread.daemon = True