Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: Python | Size: 1.01 KB | Hits: 27 | Expires: Never
Copy text to clipboard
  1. #!/usr/bin/env python
  2.  
  3. import threading
  4. import time
  5.  
  6. class TwitterUpdater(threading.Thread):
  7.     def __init__(self, shared_data, *args, **kargs):
  8.         threading.Thread.__init__(self)
  9.         self.shared_data = shared_data
  10.         self.stop = False
  11.    
  12.     def run(self):
  13.         # This is where we run the main loop
  14.         # -- Do the streaming twitter stuff here?
  15.         i = 1
  16.         while not self.stop:
  17.             time.sleep(1)
  18.             self.shared_data['something'] = i
  19.             i += 1
  20.            
  21.     def join(self):
  22.         self.stop = True
  23.         threading.Thread.join(self)
  24.    
  25. if __name__ == "__main__":
  26.     try:
  27.         # Create our shared data
  28.         d = {'something': 0}
  29.         # Setup the thread
  30.         twitter_updater = TwitterUpdater(d)
  31.         # Start the thread
  32.         twitter_updater.start()
  33.         for i in range(100):
  34.             print d['something']
  35.             time.sleep(0.2)
  36.        
  37.     finally:
  38.         # Stop the thread
  39.         twitter_updater.join()