Created
August 31, 2012 17:52
-
-
Save colinpollock/3556496 to your computer and use it in GitHub Desktop.
python3.2 futures
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
#!/usr/bin/env python3.2 | |
from concurrent.futures import ThreadPoolExecutor | |
from itertools import chain | |
import time | |
import requests | |
def ngrams(tokens, n): | |
for start in range(0, len(tokens) - n + 1): | |
yield ' '.join(tokens[start: start + n]) | |
def make_req(query): | |
return requests.get("http://www.google.com", params={'q': query}) | |
words = """cat dog food bread television couch guitar soup horchata girlfriend | |
laptop phone ping pong paddle tennis ball tree pizza soup | |
""".split() | |
queries = list(chain(words, ngrams(words, 2), ngrams(words, 3), ngrams(words, 4))) | |
print('%d queries' % len(queries)) | |
# 90 queries | |
start = time.time() | |
for query in queries: | |
make_req(query) | |
print('Without futures:', time.time() - start) | |
# Without futures: 17.00494694709778 | |
start = time.time() | |
with ThreadPoolExecutor(max_workers=8) as e: | |
for query in queries: | |
e.submit(make_req, query) | |
print('With futures:', time.time() - start) | |
# With futures: 4.0052080154418945 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment