|
| 1 | +import asyncio |
| 2 | +from datetime import datetime |
| 3 | +import logging |
| 4 | + |
| 5 | +import aiohttp |
| 6 | +import requests |
| 7 | +import certifi |
| 8 | + |
| 9 | + |
| 10 | +TIMEOUT_FOR_PAR_REQS = 60*5 |
| 11 | +TIMEOUT_FOR_SEQ_REQS = 60 |
| 12 | +Logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +class InvalidClientResponse: |
| 16 | + def __init__(self): |
| 17 | + self.status = None |
| 18 | + |
| 19 | + |
| 20 | +async def fetch(uri, session, body=False): |
| 21 | + """ |
| 22 | + asynchronous `get` or `head` |
| 23 | + """ |
| 24 | + start = datetime.utcnow() |
| 25 | + do_request = session.head if body is False else session.get |
| 26 | + logging.info(uri) |
| 27 | + try: |
| 28 | + async with do_request(uri) as response: |
| 29 | + text = await response.text() |
| 30 | + except ( |
| 31 | + aiohttp.ClientResponseError, |
| 32 | + aiohttp.ClientError, |
| 33 | + AttributeError) as e: |
| 34 | + response = InvalidClientResponse() |
| 35 | + Logger.exception(e) |
| 36 | + text = None |
| 37 | + finally: |
| 38 | + # acrescenta novos atributos para o objeto ClientResponse |
| 39 | + response.uri = uri |
| 40 | + response.end_time = datetime.utcnow() |
| 41 | + response.start_time = start |
| 42 | + response.status_code = response.status |
| 43 | + response.text = text |
| 44 | + |
| 45 | + Logger.info("Requested %s: %s", uri, response.status_code) |
| 46 | + return response |
| 47 | + |
| 48 | + |
| 49 | +async def fetch_many(uri_items, body=False, timeout=TIMEOUT_FOR_PAR_REQS): |
| 50 | + # https://docs.aiohttp.org/en/stable/client_quickstart.html#timeouts |
| 51 | + client_timeout = aiohttp.ClientTimeout(total=timeout or TIMEOUT_FOR_PAR_REQS) |
| 52 | + async with aiohttp.ClientSession(timeout=client_timeout) as session: |
| 53 | + responses = await asyncio.gather(*[ |
| 54 | + fetch(url, session, body) |
| 55 | + for url in uri_items |
| 56 | + ]) |
| 57 | + return responses |
| 58 | + |
| 59 | + |
| 60 | +def simultaneous_requests(uri_items, body=False, timeout=TIMEOUT_FOR_PAR_REQS) -> object: |
| 61 | + """ |
| 62 | + performs simultaneous requests |
| 63 | + """ |
| 64 | + return asyncio.run(fetch_many(uri_items, body, timeout=timeout)) |
| 65 | + |
| 66 | + |
| 67 | +def seq_requests(uri_items, body=False, timeout=TIMEOUT_FOR_SEQ_REQS): |
| 68 | + """ |
| 69 | + performs sequential requests |
| 70 | + """ |
| 71 | + do_request = requests.head if body is False else requests.get |
| 72 | + resps = [] |
| 73 | + for u in uri_items: |
| 74 | + logging.info(u) |
| 75 | + resp = do_request(u, timeout=timeout) |
| 76 | + resps.append(resp) |
| 77 | + return resps |
| 78 | + |
| 79 | + |
| 80 | +def compare(lista, body=True): |
| 81 | + print("") |
| 82 | + print("Body: {}".format(body)) |
| 83 | + print("Requests: ") |
| 84 | + print("\n".join(lista)) |
| 85 | + |
| 86 | + resps = {} |
| 87 | + for name, func in (("Sequential", seq_requests), ("Parallel", simultaneous_requests)): |
| 88 | + print(name) |
| 89 | + t1 = datetime.utcnow() |
| 90 | + items = func(lista, body) |
| 91 | + t2 = datetime.utcnow() |
| 92 | + resps[name] = { |
| 93 | + "duration (ms)": (t2 - t1).microseconds, |
| 94 | + "status": [r.status_code for r in items], |
| 95 | + "len": [len(r.text) for r in items], |
| 96 | + } |
| 97 | + print(resps) |
| 98 | + print(resps["Parallel"]["duration (ms)"] / resps["Sequential"]["duration (ms)"]) |
| 99 | + |
| 100 | + |
| 101 | +def main(): |
| 102 | + lista3 = [ |
| 103 | + 'https://www.scielo.br/scielo.php?script=sci_arttext&pid=S0102-67202020000200304&lng=en&nrm=iso&tlng=en', |
| 104 | + 'https://www.scielo.br/scielo.php?script=sci_arttext&pid=S0102-67202020000200305&lng=en&nrm=iso', |
| 105 | + 'https://www.scielo.br/scielo.php?pid=S0100-39842020000200001&script=sci_arttext&tlng=pt', |
| 106 | + ] |
| 107 | + |
| 108 | + compare(lista3, True) |
| 109 | + compare(lista3, False) |
| 110 | + compare([lista3[0]], True) |
| 111 | + compare([lista3[0]], False) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + main() |
0 commit comments