Skip to main content

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                            # Daemonize thread
        thread.start()                                  # Start the execution


    def stop(self):
        #function for stoping thread
        print "thread stopped"
        self._stop.set()

    def stopped(self):
        #function for check if thread is stopped or not
        return self._stop.isSet()

       
    def run(self):
        """ Method that runs forever """
        while True:
            # Do something
            print('Doing something imporant in the background')
            time.sleep(self.interval)
            print self.stopped()




example = ThreadingExample()
time.sleep(3)
print('Checkpoint')
time.sleep(2)
print('Bye')
example.stop()
print example.stopped()
#######################


Dont just copy and paste the code because there are some indent 
problem occured due to web view so please type and learn if you wanted to learn how this is work let me know in the comment section.

please subscribe to my YouTube channel :

Comments

Popular posts from this blog

How to install swift in arch linux

IN aur i have tried 3 days off. oh shit no errors and errors finally found this method not appropriate. but till aur errors resolved so this it. Fish shell users: set -x LD_LIBRARY_PATH  "download path" (do not include usr directory ) you have to set variable for everytime you want to run swift but there is a method for universal environmental variable. bash users: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<download path> thats it . this is how you can run swift in any linux machine. till official support announce. thank you. My Apps Android: P Music Player  Find Mechanics near You  (my startup) Please download  it will appreciate. and comment me for improvement.

Python Reads gmail

This is tutorial on how to read gmail emails with python programme : ----------------- #!/usr/bin/python __author__ = 'Robokishan' import email import imaplib import ctypes import getpass mail = imaplib.IMAP4_SSL('imap.gmail.com',993) unm = raw_input("insert Email : ") #pwd = raw_input("insert password : ") pwd = getpass.getpass("input : ") mail.login(unm,pwd) mail.select("INBOX") def loop():    mail.select("INBOX")    n=0    (retcode, messages) = mail.search(None, '(UNSEEN)')    if retcode == 'OK':             for num in messages[0].split() :          #print 'Processing '          n=n+1          print n          typ, data = mail.fetch(num,'(RFC822)')          for response_part in data:             if isinstance(response_part, tuple):                 original = email.message_from_string(response_part[1])                 print original['From']        

All Android Manifest permissions

<uses-permission android:name="android.permission.ACCESS_CACHE_FILESYSTEM"/> <uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY"/> <uses-permission android:name="android.permission.ACCESS_DRM_CERTIFICATES"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FM_RADIO"/> <uses-permission android:name="android.permission.ACCESS_IMS_CALL_SERVICE"/> <uses-permission android:name="android.permission.ACCESS_INPUT_FLINGER"/> <uses-permission android:name="android.permission.ACCESS_KEYGUARD_SECURE_STORAGE"/> <uses-permission android:name="an