Created
August 5, 2012 06:16
-
-
Save pranavraja/3262194 to your computer and use it in GitHub Desktop.
Python3 concurrency
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
# Playing around with the `concurrent.futures` module | |
# Usage: | |
# python concurrent_test.py [number_of_requests=10] | |
# | |
from __future__ import print_function | |
from concurrent.futures import ThreadPoolExecutor | |
import requests | |
import time | |
import sys | |
def request(__): | |
requests.get('http://www.google.com') | |
def pmap(fn, lst): | |
executor = ThreadPoolExecutor(max_workers=8) | |
return executor.map(fn, lst) | |
def timeit(fn, number=1, mapper=map): | |
start = time.time() | |
list(mapper(fn, range(number))) # Force evaluation of generator | |
return time.time() - start | |
if __name__ == '__main__': | |
number_of_requests = int(sys.argv[1]) if len(sys.argv) > 1 else 10 | |
print('Series ({} requests):'.format(number_of_requests), | |
timeit(request, number_of_requests)) | |
print('Parallel ({} requests):'.format(number_of_requests), | |
timeit(request, number_of_requests, mapper=pmap)) |
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
argparse==1.2.1 | |
distribute==0.6.24 | |
futures==2.1.2 | |
requests==0.13.5 | |
wsgiref==0.1.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment