Advertisement
nux95

Threading example

Jan 24th, 2012
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. import time
  2. import threading
  3.  
  4.  
  5. # Threaded function-call -------------------------------------------------------
  6.  
  7. def counter(x):
  8.     for i in xrange(x):
  9.         print i
  10.         time.sleep(1)
  11.  
  12. t = threading.Thread(target = counter, args = [30])
  13. t.start()
  14.  
  15.  
  16.  
  17. # Thread-class subclassing -----------------------------------------------------
  18.  
  19. class CounterThread(threading.Thread):
  20.  
  21.     def __init__(self, x):
  22.         super(CounterThread, self).__init__()   # DO NOT FORGET THIS !
  23.         self.x = x
  24.  
  25.     def run(self):
  26.         for i in xrange(self.x):
  27.             print i
  28.             time.sleep(1)
  29.  
  30. t = CounterThread(30)
  31. t.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement