# DotsAndDollars.py Copyright (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-05-06 File created. # 2022-12-18 Converted to Python 3. # Note that it may take 4 seconds before this program stops # after it has been ordered to stop. # With the sleep() function of the time module it is possible # to generate delays in a thread. The agrument that is given # to the sleep() function can be a floating-point number. from threading import Thread from time import sleep class ThreadToPrintDots ( Thread ) : def __init__( self ) : Thread.__init__( self ) self.must_print_dots = True def stop_this_thread( self ) : self.must_print_dots = False def run( self ) : while self.must_print_dots == True : sleep( 1.000 ) # Wait one second. print( " ." ) class ThreadToPrintDollarSigns ( Thread ) : def __init__( self ) : Thread.__init__( self ) self.must_print_dollar_signs = True def stop_this_thread( self ) : self.must_print_dollar_signs = False def run( self ) : while self.must_print_dollar_signs == True : sleep( 4.050 ) # Wait 4.05 seconds. print( " $" ) thread_to_print_dots = ThreadToPrintDots() thread_to_print_dollar_signs = ThreadToPrintDollarSigns() thread_to_print_dots.start() thread_to_print_dollar_signs.start() print( "\n Press the Enter key to stop the program. \n\n" ) any_string_from_keyboard = input() thread_to_print_dots.stop_this_thread() thread_to_print_dollar_signs.stop_this_thread()