Skip to content

Instantly share code, notes, and snippets.

@jameskyle
Created May 3, 2016 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameskyle/ecdebbe00644bc69c96196cc60b46a3b to your computer and use it in GitHub Desktop.
Save jameskyle/ecdebbe00644bc69c96196cc60b46a3b to your computer and use it in GitHub Desktop.
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