Stopping a Thread with Time Slicing

Steve sreisscruz at gmail.com
Thu Oct 2 19:03:16 EDT 2008


Hi All,

I've been trying to come up with a good way to run a certain process
at a timed interval (say every 5 mins) using the SLEEP command and a
semaphore flag. The basic thread loop was always sitting in the sleep
command and not able to be interrupted. When the time came to set the
semaphore flag to false (stopping the thread), my program would have
to wait up to the entire sleep time to break out of the loop.

I have finally found a very workable solution to break out of the
sleep loop by using a time slicing loop to divide the overall sleep
time into small pieces (slices) giving the loop more opportunities to
be interrupted.


Here is my code :

-------------------------------------------------------------

import time
import datetime
import threading


def log(message):

  now = datetime.datetime.now().strftime("%H:%M:%S")
  print "%s :  %s" % (now, message)



class StoppableThread(threading.Thread):

  def __init__(self, sleep_time, time_slice):

    self.sleep_time = sleep_time
    self.running = True
    self.time_slice = time_slice

    threading.Thread.__init__(self)


  def run(self):

    while self.running:

      log('Thread Running...')
      #process_some_data()  ##  <<-- Call some def here to perform
some timed task
      log('## Do Some Work Here ##\n')

      # Sleep Loop :
      # Put sleeps in time_slice loop, with sleep = sleep_time /
time_slice
      # This gives the sleep loop more opportunities to be interrupted
      # when the running flag is set to False

      for current_loop in range(0, self.time_slice) :

        time.sleep(self.sleep_time / self.time_slice)

        if not self.running:    # check the flag
          break                 # break out of the sleep loop


    log('** Thread Has STOPPED!')


  def stop(self):                 # stop the thread from running
    self.running = False


#####################################################################
#    T E S T
#####################################################################

SMALL_SLEEP = 35
CHECK_SLEEP = 300       # sleep interval in seconds to run a timed
process
TIME_SLICE = 100        # number of slices to divide CHECK_TIME
(higher number = faster kill response)


log('Create Thread')
thread_obj = StoppableThread(CHECK_SLEEP, TIME_SLICE)

log('Thread Start\n')
thread_obj.start()

for current_loop in range(0,10):
  time.sleep(SMALL_SLEEP)
  log('current loop = %d \n' % current_loop)

log('Thread Stop')
thread_obj.stop()

log('Done!')


--------------------------------------------

Test Results :

>python Simple_Thread.py

15:37:23 :  Create Thread
15:37:23 :  Thread Start

15:37:23 :  Thread Running...
15:37:23 :  ## Do Some Work Here ##

15:37:58 :  current loop = 0

15:38:33 :  current loop = 1

15:39:08 :  current loop = 2

15:39:43 :  current loop = 3

15:40:18 :  current loop = 4

15:40:53 :  current loop = 5

15:41:28 :  current loop = 6

15:42:03 :  current loop = 7

15:42:23 :  Thread Running...
15:42:23 :  ## Do Some Work Here ##

15:42:38 :  current loop = 8

15:43:13 :  current loop = 9

15:43:13 :  Thread Stop
15:43:13 :  Done!
15:43:14 :  ** Thread Has STOPPED!



More information about the Python-list mailing list