Last active
December 21, 2015 23:48
-
-
Save gsong/6384563 to your computer and use it in GitHub Desktop.
Pythonista demo of a single worker thread which polls a website for updates and displays the result in `Scene.draw`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
import Queue | |
import re | |
import threading | |
import urllib2 | |
from scene import * | |
OUTPUT_TEMPLATE = u"""\ | |
Number of threads: {} | |
Queue size: {} | |
Frame number: {} | |
Last polled: {} | |
Current time: {} | |
""" | |
# Thread worker | |
def worker(q): | |
URL = 'http://tycho.usno.navy.mil/cgi-bin/timer.pl' | |
RE_TIME = re.compile('<BR>(.*?)\t.*Universal Time') | |
while True: | |
obj = q.get() | |
html = urllib2.urlopen(URL).read() | |
obj.text = RE_TIME.search(html).group(1) | |
q.task_done() | |
class ThreadedSceneDemo(Scene): | |
def setup(self): | |
self.text = 'Waiting...' | |
self.frame = 0 | |
self.q = Queue.Queue(1) | |
# Start a single worker thread | |
t = threading.Thread(target=worker, args=(self.q,)) | |
t.daemon = True | |
t.start() | |
def draw(self): | |
# This will be called for every frame (typically 60 times per second). | |
self.frame += 1 | |
now = datetime.utcnow().strftime('%b. %d, %H:%M:%S UTC') | |
# Schedule another poll if the worker finished processing the last job | |
if self.q.empty(): | |
self.q.put(self) | |
output = OUTPUT_TEMPLATE.format( | |
threading.active_count(), self.q.qsize(), self.frame, self.text, | |
now | |
) | |
background(0, 0, 0) | |
text(output, alignment=9) | |
if __name__ == '__main__': | |
run(ThreadedSceneDemo()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The URL is not getting closed. if garbage collection does not happen fast enough, the threads might get into a race condition. See http://stackoverflow.com/questions/3880750/closing-files-properly-opened-with-urllib2-urlopen
You might consider replacing:
with: