Skip to content

Instantly share code, notes, and snippets.

@gsong

gsong/ThreadedSceneDemo.py

Last active Dec 21, 2015
Embed
What would you like to do?
Pythonista demo of a single worker thread which polls a website for updates and displays the result in `Scene.draw`.
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())
@cclauss

This comment has been minimized.

Copy link

@cclauss cclauss commented Sep 1, 2013

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:

    html = urllib2.urlopen(URL).read()

with:

from contextlib import closing

    with closing(urllib2.urlopen(URL)) as source:
         html = source.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment