Curses programming, threads?

Michele Simionato michele.simionato at gmail.com
Fri Dec 3 09:50:36 EST 2004


Bartlomiej Rymarski <bart at oceanic.wsisiz.edu.pl> wrote in message news:<copgb5$uus$2 at portraits.wsisiz.edu.pl>...
> Bartlomiej Rymarski <bart at oceanic.wsisiz.edu.pl> wrote:
> > [...]
> > And the loader() function would run in a loop until connect_db() is 
> > is finished. Is that possible in python? Or are there any other,
> > better ways to do it?
> > [...]
> 
> Oh, I forgot - I'm using Linux, and curses module in Python 2.3.

You don't need curses. Some time ago somebody (I forgot the name)
posted this spinner class:

class Spinner( threading.Thread ): # a google search should find the author

    DELAY = 0.1
    DISPLAY = [ '|', '/', '-', '\\' ]
    
    def __init__( self, before='', after='' ):
        threading.Thread.__init__( self )
        self.before = before
        self.after = after
    
    def run( self ):
        write, flush = sys.stdout.write, sys.stdout.flush
        self.running = 1
        pos = -1
        while self.running:
            pos = (pos + 1) % len(self.DISPLAY)
            msg = self.before + self.DISPLAY[pos] + self.after
            write( msg )
            flush()
            write( '\x08' * len(msg) )
            time.sleep( self.DELAY )
        write( ' ' * len(msg) + '\x08' * len(msg) )
        flush()
        
    def stop( self ):
        self.running = 0
        self.join()

if __name__=="__main__":
    spinner = Spinner('Be patient please ...')
    spinner.start()
    time.sleep(5) # doing a long operation
    spinner.stop()



             Michele Simionato



More information about the Python-list mailing list