Untitled
By: a guest | Feb 9th, 2010 | Syntax:
Python | Size: 1.26 KB | Hits: 10 | Expires: Never
#!/usr/bin/env python
import threading
import time
called = False
class TwitterUpdater(threading.Thread):
def __init__(self, callback, *args, **kargs):
threading.Thread.__init__(self)
self.callback = callback
self.args = args
self.kargs = kargs
self.stop = False
def run(self):
# This is where we run the main loop
# -- Do the streaming twitter stuff here?
time.sleep(1)
self.callback(*self.args, **self.kargs)
def join(self):
self.stop = True
threading.Thread.join(self)
def my_function(*args, **kargs):
# At the moment we import the global variable 'called', but we could just
# as easily make my_function a class function which stores data there
global called
called = True
if __name__ == "__main__":
try:
# Setup the thread
twitter_updater = TwitterUpdater(my_function)
# Start the thread
twitter_updater.start()
for i in range(10):
time.sleep(0.2)
if called:
print "Called!"
else:
print "Not called yet :("
finally:
# Stop the thread
twitter_updater.join()