Skip to content

Instantly share code, notes, and snippets.

@sssionggg
Created October 4, 2011 20:17
Show Gist options
  • Save sssionggg/1262669 to your computer and use it in GitHub Desktop.
Save sssionggg/1262669 to your computer and use it in GitHub Desktop.
gevent vs thread
#!/usr/bin/python
# Copyright (c) 2009 Denis Bilenko. See LICENSE for details.
"""Spawn multiple workers and wait for them to complete"""
hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com", "http://ibm.com", "http://apple.com"]
import gevent
from gevent import monkey
# patches stdlib (including socket and ssl modules) to cooperate with other greenlets
monkey.patch_all()
import urllib2
import time
def print_head(url):
data = urllib2.urlopen(url).read()
print ('%s: %s bytes: %r' % (url, len(data), data[:50]))
start = time.time()
def main():
jobs = [gevent.spawn(print_head, host) for host in hosts]
gevent.joinall(jobs)
main()
print "Elapsed Time: %s" % (time.time() - start)
#!/usr/bin/env python
import Queue
import threading
import urllib2
import time
hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com",
"http://ibm.com", "http://apple.com"]
queue = Queue.Queue()
class ThreadUrl(threading.Thread):
"""Threaded Url Grab"""
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
#grabs host from queue
host = self.queue.get()
data = urllib2.urlopen(host).read()
print ('%s: %s bytes: %r' % (host, len(data), data[:50]))
#signals to queue job is done
self.queue.task_done()
start = time.time()
def main():
for i in range(5):
t = ThreadUrl(queue)
t.setDaemon(True)
t.start()
for host in hosts:
queue.put(host)
queue.join()
main()
print "Elapsed Time: %s" % (time.time() - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment