Last active
May 28, 2017 01:47
-
-
Save drgarcia1986/2a5d283b0d279ea96c26 to your computer and use it in GitHub Desktop.
Threads em Python? é claro! http://www.diego-garcia.info/2016/02/18/threads-em-python-e-claro
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
from concurrent.futures import as_completed, ThreadPoolExecutor | |
from urllib.request import urlopen | |
import re | |
import sys | |
DEFAULT_REGEX = r'<input type="text" id="nacional" value="([^"]+)"/>' | |
CURRENCY = { | |
'dolar': 'http://dolarhoje.com/', | |
'euro': 'http://eurohoje.com/', | |
'libra': 'http://librahoje.com/', | |
'peso': 'http://pesohoje.com/' | |
} | |
def exchange_rate(url): | |
response = urlopen(url).read().decode('utf-8') | |
result = re.search(DEFAULT_REGEX, response) | |
if result: | |
return result.group(1) | |
def run_threads(): | |
with ThreadPoolExecutor(max_workers=len(CURRENCY)) as executor: | |
waits = { | |
executor.submit(exchange_rate, url): currency | |
for currency, url in CURRENCY.items() | |
} | |
for future in as_completed(waits): | |
currency = waits[future] | |
print('{}: R${}'.format(currency, future.result())) | |
def run_serial(): | |
for currency, url in CURRENCY.items(): | |
print('{}: R${}'.format(currency, exchange_rate(url))) | |
if __name__ == '__main__': | |
""" | |
To run serial | |
$ python multi_requests.py | |
To run multithread | |
$ python multi_requests.py threads | |
""" | |
if len(sys.argv) > 1 and sys.argv[1] == 'threads': | |
run_threads() | |
else: | |
run_serial() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment