pausing a thread

Doug Fort dougfort at dougfort.net
Thu Jul 5 09:05:20 EDT 2001


Daniel Nuriyev wrote:

> Hello
> This is a Java programmer's question
> How do you say:
> class QQQ extends Thread{
>     public void run(){
>         ...
>         sleep(ms);
>     }
> }
> in Python? Thank you all.
> 

I have a little pattern, or idiom that I use on many threads.  
I like to wait on the threading 'Event' object rather than the 'sleep'
timer, because it gives me the ability to break out without waiting
for the full interval. 

import threading

class QQQ(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self, name="QQQ")
        self._stopevent = threading.Event()

    def run(self):
        while not self._stopevent.isSet():
            # do some stuff
            self._stopevent.wait( s )

    def join(self, timeout=0.0):
        self._stopevent.set()
        threading.Thread.join(self, timeout)
-- 
Doug Fort <dougfort at dougfort.net>
http://www.dougfort.net

______________________________________________________________________
Posted Via Uncensored-News.Com - Still Only $9.95 - http://www.uncensored-news.com
   With Seven Servers In California And Texas - The Worlds Uncensored News Source
  



More information about the Python-list mailing list