Created
May 3, 2016 20:57
-
-
Save jameskyle/ecdebbe00644bc69c96196cc60b46a3b to your computer and use it in GitHub Desktop.
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
import requests | |
import time | |
import gevent | |
from gevent.queue import JoinableQueue | |
class Consumer(object): | |
def __init__(self): | |
self.queue = JoinableQueue() | |
self.producers = [Producer(self.queue) for _ in range(10)] | |
def start(self): | |
self.greenlets = [gevent.spawn(c.queries) for c in self.producers] | |
self.process() | |
def process(self): | |
while True: | |
item = self.queue.get() | |
print(item) | |
class Producer(object): | |
def __init__(self, queue): | |
self.queue = queue | |
def queries(self): | |
while True: | |
resp = requests.get("http://www.google.com") | |
self.queue.put(resp.text) | |
time.sleep(1) | |
if __name__ == "__main__": | |
p = Producer() | |
p.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment