Created
May 11, 2012 18:33
-
-
Save presci/2661566 to your computer and use it in GitHub Desktop.
Semaphore example
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 threading, os, re, sqlite3, sys | |
from subprocess import * | |
class GrabUrl(threading.Thread): | |
def __init__(self, arg0): | |
threading.Thread.__init__(self) | |
self.host=arg0 | |
def run(self): | |
print "Getting " + self.host | |
arr=['wget', self.host] | |
p1=Popen(arr, shell=False) | |
waiter=Process_Waiter(p1, self.host) | |
waiter.start() | |
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() | |
class Process_Waiter(threading.Thread): | |
def __init__(self, arg0, arg1): | |
threading.Thread.__init__(self) | |
self.p1=arg0 | |
self.host=arg1 | |
def run(self): | |
try: | |
self.p1.wait() | |
finally: | |
print "exiting " + self.host | |
pool.release() | |
maxconn=2 | |
pool=threading.BoundedSemaphore(value=maxconn) | |
hosts=["http://google.com", "http://amazon.com", "http://apple.com", "http://aol.com", "http://yahoo.com", "http://yahoo.co.uk", "http://ebay.com"] | |
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