experiments

All kinds of coding experiments
Log | Files | Refs | Submodules

ThreadingTest.py (834B)


      1 from threading import Thread
      2 
      3 class iterator(Thread):
      4     def __init__(self):
      5         Thread.__init__(self)
      6         self.daemon = True      # When only daemon threads are running, the program exits
      7     def run(self):              # This function is called when the <object>.start() is executed.
      8         x = 0
      9         self.y = 0
     10         while True:
     11             x += 1
     12             if x == 5000000:
     13                 x = 0
     14                 self.y += 1
     15 
     16 if __name__ == '__main__':
     17     saveit = iterator()
     18     saveit.start()
     19     print(  """Commands:
     20                 \n\tit\t- Print number of iterations from thread\
     21                 \n\tquit\t- Quit program
     22             """)
     23     while True:
     24         invar = input('> ')
     25         if invar == 'quit':
     26             break
     27         elif invar == 'it':
     28             print('Iterations: ' + str(saveit.y))