diff options
author | brian.quinlan <devnull@localhost> | 2009-05-04 21:26:33 +0000 |
---|---|---|
committer | brian.quinlan <devnull@localhost> | 2009-05-04 21:26:33 +0000 |
commit | 18bb9b9bdb0889127e2ffeb6a6893bf0e5f38bf1 (patch) | |
tree | b9c917508aa7c35fb864879d0392ed86b4922f00 | |
parent | a12b38ce97773a42379c032cb4003411c094c030 (diff) | |
download | futures-18bb9b9bdb0889127e2ffeb6a6893bf0e5f38bf1.tar.gz |
Some example love.
-rw-r--r-- | crawl.py | 44 | ||||
-rw-r--r-- | primes.py | 9 |
2 files changed, 38 insertions, 15 deletions
@@ -11,12 +11,12 @@ URLS = ['http://www.google.com/', 'http://www.thisurlprobablydoesnotexist.com', 'http://www.slashdot.org/', 'http://www.python.org/', - 'http://www.sweetapp.com/'] * 1000 + 'http://www.sweetapp.com/'] * 5 def load_url(url, timeout): return urllib.request.urlopen(url, timeout=timeout).read() -def download_urls(urls, timeout=60): +def download_urls_sequential(urls, timeout=60): url_to_content = {} for url in urls: try: @@ -25,14 +25,34 @@ def download_urls(urls, timeout=60): pass return url_to_content -executor = futures.ProcessPoolExecutor(100) -def download_urls_with_futures(urls, timeout=60): - url_to_content = {} - fs = executor.run( - (functools.partial(load_url, url, timeout) for url in urls), - timeout=timeout) - for url, future in zip(urls, fs.successful_futures()): - url_to_content[url] = future.result() - return url_to_content +def download_urls_with_executor(urls, executor, timeout=60): + try: + url_to_content = {} + fs = executor.run( + (functools.partial(load_url, url, timeout) for url in urls), + timeout=timeout) + for url, future in zip(urls, fs.successful_futures()): + url_to_content[url] = future.result() + return url_to_content + finally: + executor.shutdown() + +import functools +import time +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)) -print(download_urls_with_futures(URLS)) +main() @@ -22,7 +22,7 @@ def sequential(): return list(map(is_prime, PRIMES)) def with_process_pool_executor(): - executor = futures.ProcessPoolExecutor() + executor = futures.ProcessPoolExecutor(10) try: return list(executor.map(is_prime, PRIMES)) finally: @@ -39,8 +39,11 @@ def main(): for name, fn in [('sequential', sequential), ('processes', with_process_pool_executor), ('threads', with_thread_pool_executor)]: + print('%s: ' % name.ljust(12), end='') start = time.time() - fn() - print('%s: %.2f seconds' % (name.ljust(10), time.time() - start)) + if fn() != [True] * len(PRIMES): + print('failed') + else: + print('%.2f seconds' % (time.time() - start)) main()
\ No newline at end of file |