Created
February 6, 2014 18:05
-
-
Save Bolt64/8849413 to your computer and use it in GitHub Desktop.
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,time,urllib.request,queue | |
class fetcher(threading.Thread): | |
def __init__(self,print_lock,url_queue,threadId): | |
self.Id=threadId | |
self.print_lock=print_lock | |
self.url_queue=url_queue | |
threading.Thread.__init__(self) | |
def run(self): | |
while not self.url_queue.empty(): | |
self.url=self.url_queue.get(block=False) | |
request = urllib.request.Request(self.url) | |
urllib.request.urlopen(request) | |
self.url_queue.task_done() | |
with self.print_lock: | |
print("{0} opened by {1}".format(self.url,self.Id)) | |
url_list_sample=[ | |
'http://www.python.org', | |
'http://www.python.org/doc', | |
'http://www.python.org/about', | |
'http://www.python.org/getit', | |
'http://www.python.org/community', | |
'http://www.python.org/psf', | |
] | |
def main(num_threads=4,url_list=url_list_sample,time_it=False): | |
start=time.time() | |
url_queue=queue.Queue() | |
print_lock=threading.Lock() | |
threads=[fetcher(print_lock,url_queue,i) for i in range(num_threads)] | |
for url in url_list: | |
url_queue.put(url,block=False) | |
for thread in threads:thread.start() | |
for thread in threads:thread.join() | |
url_queue.join() | |
stop=time.time() | |
if time_it: | |
print(stop-start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment