Stopping a Thread with Time Slicing

Steve sreisscruz at gmail.com
Fri Oct 3 16:41:46 EDT 2008


Hi Todd,

Thanks for your suggestions on using the Event condition methods on
this thread.

Here is my updated 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, function, args=[], kwargs={}):

    self.sleep_time = sleep_time

    threading.Thread.__init__(self)

    self.function = function
    self.args = args
    self.kwargs = kwargs
    self.finished = threading.Event()


  def run(self):

    while not self.finished.isSet():            # loop while condition
is true
      log('** Doing Work')
      self.function(*self.args, **self.kwargs)  # run the function
      self.finished.wait(self.sleep_time)       # put thread in wait
state


    log('** Thread Has STOPPED!')


  def stop(self):                 # stop the thread from running
    log('* Stopping Thread')
    self.finished.set()
    self.join()




def my_function (a, b, c):
  log('my_function running... %s' % a)


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

SMALL_SLEEP = 35
CHECK_SLEEP = 300       # sleep interval in seconds to run a timed
process


log('Create Thread')
thread_obj = StoppableThread(CHECK_SLEEP, my_function, (15,0,-1))


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('Call Thread Stop')
thread_obj.stop()

log('Done!')


Test Output :

>python Simple_Thread_Stop_Event.py

12:58:42 :  Create Thread
12:58:42 :  Thread Start

12:58:42 :  ** Doing Work
12:58:42 :  my_function running... 15
12:59:17 :  current loop = 0
12:59:52 :  current loop = 1
13:00:27 :  current loop = 2
13:01:02 :  current loop = 3
13:01:37 :  current loop = 4
13:02:12 :  current loop = 5
13:02:47 :  current loop = 6
13:03:22 :  current loop = 7
13:03:42 :  ** Doing Work
13:03:42 :  my_function running... 15
13:03:57 :  current loop = 8

13:04:32 :  current loop = 9

13:04:32 :  Call Thread Stop
13:04:32 :  * Stopping Thread
13:04:32 :  ** Thread Has STOPPED!
13:04:32 :  Done!


On Oct 2, 4:40 pm, Todd Whiteman <to... at activestate.com> wrote:
> Steve wrote:
>
> A better approach for this is to use a Python Event or Condition object:http://docs.python.org/library/threading.html#id5
>
> Example code:
>
> import threading
> my_stop_event = threading.Event()
>
>        # Sleep Loop :
>        #for current_loop in range(0, self.time_slice) :
>        #  time.sleep(self.sleep_time / self.time_slice)
>
>        event.wait(self.sleep_time)
>        if not self.running:    # check the flag
>          break                 # break out of the sleep loop
>
> # From another thread, you can notify the above sleeping thread using:
> my_stop_event.set()
>
> Cheers,
> Todd




More information about the Python-list mailing list