Skip to content

Instantly share code, notes, and snippets.

@gsong gsong/

Last active Dec 21, 2015
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 *
Number of threads: {}
Queue size: {}
Frame number: {}
Last polled: {}
Current time: {}
# Thread worker
def worker(q):
URL = ''
RE_TIME = re.compile('<BR>(.*?)\t.*Universal Time')
while True:
obj = q.get()
html = urllib2.urlopen(URL).read()
obj.text =
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
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():
output = OUTPUT_TEMPLATE.format(
threading.active_count(), self.q.qsize(), self.frame, self.text,
background(0, 0, 0)
text(output, alignment=9)
if __name__ == '__main__':

This comment has been minimized.

Copy link

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

You might consider replacing:

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


from contextlib import closing

    with closing(urllib2.urlopen(URL)) as source:
         html =
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.