Last active
January 18, 2019 21:23
-
-
Save centaurialpha/e5c8f994eddeac76110dde4e6d8d84af to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import argparse | |
| import time | |
| import requests | |
| from concurrent.futures import ThreadPoolExecutor | |
| def main(args): | |
| URLS = [ | |
| 'http://google.com.ar', | |
| 'http://reddit.com', | |
| 'http://imgur.com', | |
| 'http://yahoo.com', | |
| 'http://unca.edu.ar' | |
| ] | |
| URLS = URLS if args.count == 5 else URLS * args.count | |
| is_async = False | |
| if args.thread: | |
| is_async = True | |
| print('Ejecutando con multithreading...') | |
| else: | |
| print('Ejecutando sin multithreading...') | |
| print(" {} URLS - {} WORKERS".format(len(URLS), args.workers)) | |
| t0 = time.time() | |
| if is_async: | |
| try: | |
| with ThreadPoolExecutor(max_workers=args.workers) as executor: | |
| for url, result in zip(URLS, executor.map(requests.get, URLS)): | |
| print(' URL: {} - result: {}'.format(url, result)) | |
| except Exception: | |
| print('Boom!') | |
| else: | |
| for url in URLS: | |
| print(' URL: {} - result: {}'.format(url, requests.get(url))) | |
| print('Terminado en {} secs'.format(time.time() - t0)) | |
| def _cliparse(): | |
| parse = argparse.ArgumentParser() | |
| parse.add_argument('-t', '--thread', action='store_true', | |
| help='Ejecutar requests con Threads') | |
| parse.add_argument('-c', '--count', default=5, help='Cantidad de URLS', type=int) | |
| parse.add_argument('-w', '--workers', default=5, help='Cantidad de Hilos', type=int) | |
| return parse.parse_args() | |
| if __name__ == '__main__': | |
| args = _cliparse() | |
| main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uso
Sin arguments
python3 request_async.pyCon arguments
python request_async.py -c 10 -t -w 10-t= Con threads-w= 10 Workers-c= 5 urls por defecto multiplicado por el valor dec= 10 URL'sSin Threads
Con Threads