Created
June 24, 2014 20:47
-
-
Save Alligator/e6b21aaf245b768975d4 to your computer and use it in GitHub Desktop.
This file contains 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
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