diff options
author | brian quinlan <brian.quinlan@gmail.com> | 2009-05-23 09:18:19 +0000 |
---|---|---|
committer | brian quinlan <brian.quinlan@gmail.com> | 2009-05-23 09:18:19 +0000 |
commit | e1c6c9758b70b3e9e1630b5e8545a9a1e3de7368 (patch) | |
tree | 2dc7cdb8c8eda8f97696e5711831143e4d3fab0d /crawl.py | |
parent | e63029bf8fa0b37f42add3d6914509dd79c81cb6 (diff) | |
download | futures-e1c6c9758b70b3e9e1630b5e8545a9a1e3de7368.tar.gz |
Seperate into python2 and python3 directories
Diffstat (limited to 'crawl.py')
-rw-r--r-- | crawl.py | 57 |
1 files changed, 0 insertions, 57 deletions
diff --git a/crawl.py b/crawl.py deleted file mode 100644 index 10e35c3..0000000 --- a/crawl.py +++ /dev/null @@ -1,57 +0,0 @@ -import datetime -import functools -import futures.thread -import time -import timeit -import urllib.request - -URLS = ['http://www.google.com/', - 'http://www.apple.com/', - 'http://www.ibm.com', - 'http://www.thisurlprobablydoesnotexist.com', - 'http://www.slashdot.org/', - 'http://www.python.org/', - 'http://www.sweetapp.com/'] * 5 - -def load_url(url, timeout): - return urllib.request.urlopen(url, timeout=timeout).read() - -def download_urls_sequential(urls, timeout=60): - url_to_content = {} - for url in urls: - try: - url_to_content[url] = load_url(url, timeout=timeout) - except: - pass - return url_to_content - -def download_urls_with_executor(urls, executor, timeout=60): - try: - url_to_content = {} - fs = executor.run_to_futures( - (functools.partial(load_url, url, timeout) for url in urls), - timeout=timeout) - for future in fs.successful_futures(): - url = urls[future.index] - url_to_content[url] = future.result() - return url_to_content - finally: - executor.shutdown() - -def main(): - for name, fn in [('sequential', - functools.partial(download_urls_sequential, URLS)), - ('processes', - functools.partial(download_urls_with_executor, - URLS, - futures.ProcessPoolExecutor(10))), - ('threads', - functools.partial(download_urls_with_executor, - URLS, - futures.ThreadPoolExecutor(10)))]: - print('%s: ' % name.ljust(12), end='') - start = time.time() - fn() - print('%.2f seconds' % (time.time() - start)) - -main() |