Created
May 11, 2012 18:34
-
-
Save presci/2661576 to your computer and use it in GitHub Desktop.
Semaphore example in python 2
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 threading | |
import urllib2 | |
import time, random | |
class GrabUrl(threading.Thread): | |
def __init__(self, arg0): | |
threading.Thread.__init__(self) | |
self.host=arg0 | |
def run(self): | |
k=random.randint(10,20) | |
print "Processing " + self.host + " waiting for : " + str(k) | |
time.sleep(k) | |
print "exiting " + self.host | |
pool.release() | |
class Handler(threading.Thread): | |
def __init__(self): | |
threading.Thread.__init__(self) | |
def run(self): | |
for i in hosts: | |
pool.acquire() | |
graburl=GrabUrl(i) | |
graburl.setDaemon(True) | |
graburl.start() | |
maxconn=2 | |
pool=threading.BoundedSemaphore(value=maxconn) | |
hosts=["http://yahoo.com", "http://google.com", "http://amazon.com", "http://ibm.com", "http://apple.com"] | |
print str(len(hosts)) | |
handler=Handler() | |
handler.start() | |
handler.join() | |
print "exiting main" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment