Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: Python | Size: 1.26 KB | Hits: 10 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. #!/usr/bin/env python
  2.  
  3. import threading
  4. import time
  5.  
  6. called = False
  7.  
  8. class TwitterUpdater(threading.Thread):
  9.     def __init__(self, callback, *args, **kargs):
  10.         threading.Thread.__init__(self)
  11.         self.callback = callback
  12.         self.args = args
  13.         self.kargs = kargs
  14.         self.stop = False
  15.    
  16.     def run(self):
  17.         # This is where we run the main loop
  18.         # -- Do the streaming twitter stuff here?
  19.         time.sleep(1)
  20.         self.callback(*self.args, **self.kargs)
  21.            
  22.     def join(self):
  23.         self.stop = True
  24.         threading.Thread.join(self)
  25.  
  26. def my_function(*args, **kargs):
  27.     # At the moment we import the global variable 'called', but we could just
  28.     # as easily make my_function a class function which stores data there
  29.     global called
  30.     called = True
  31.    
  32. if __name__ == "__main__":
  33.     try:
  34.         # Setup the thread
  35.         twitter_updater = TwitterUpdater(my_function)
  36.         # Start the thread
  37.         twitter_updater.start()
  38.         for i in range(10):
  39.             time.sleep(0.2)
  40.             if called:
  41.                 print "Called!"
  42.             else:
  43.                 print "Not called yet :("
  44.        
  45.     finally:
  46.         # Stop the thread
  47.         twitter_updater.join()