Skip to content

Instantly share code, notes, and snippets.

@Alligator
Created June 24, 2014 20:47
Show Gist options
  • Save Alligator/e6b21aaf245b768975d4 to your computer and use it in GitHub Desktop.
Save Alligator/e6b21aaf245b768975d4 to your computer and use it in GitHub Desktop.
import Queue as queue # like really
import urllib2
import threading
class DoThing(object):
def __init__(self, url):
self.q = queue.Queue()
self.t = threading.Thread(target=self.go, args=(url,))
self.t.start()
def go(self, url):
# while True:
for i in range(5): # just using a range for testing
resp = urllib2.urlopen(url)
data = resp.read()
# do stuff with data w/e
self.q.put(data)
def __iter__(self):
return self
def next(self):
try:
d = self.q.get_nowait()
return d
except queue.Empty:
return None
if __name__ == '__main__':
d = DoThing('https://gist.githubusercontent.com/Alligator/0924360bfe9411604c77/raw/7eb762ad3814b04254b4f3a25598ced3addbb2bd/ri.md')
for n in d:
if n:
print n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment