|
| 1 | +import requests |
| 2 | +import logging |
| 3 | +import time |
| 4 | +import asyncio |
| 5 | +import aiohttp |
| 6 | + |
| 7 | +logging.basicConfig( |
| 8 | + format="%(levelname)s @ %(asctime)s : %(message)s", |
| 9 | + datefmt="%d.%m.%Y %H:%M:%S", |
| 10 | + level=logging.INFO, |
| 11 | + handlers=[logging.FileHandler("requests.log", mode="w"), logging.StreamHandler()], |
| 12 | +) |
| 13 | +HEADERS = { |
| 14 | + "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 12_6) " |
| 15 | + "AppleWebKit/537.36 (KHTML, like Gecko) " |
| 16 | + "Chrome/106.0.0.0 Safari/537.36" |
| 17 | +} |
| 18 | +BASE_URL = "https://www.canbula.com/prime" |
| 19 | +MAX_RETRIES = 3 |
| 20 | +TIMEOUT = 10 |
| 21 | + |
| 22 | + |
| 23 | +def sync_request(session: requests.Session, n: int) -> dict: |
| 24 | + """Synchronous request with error handling, logging, and retry logic""" |
| 25 | + for attempt in range(MAX_RETRIES): |
| 26 | + try: |
| 27 | + response = session.get(f"{BASE_URL}/{n}", headers=HEADERS, timeout=TIMEOUT) |
| 28 | + response.raise_for_status() |
| 29 | + logging.info( |
| 30 | + f"Request returned for {n} with status code {response.status_code}" |
| 31 | + ) |
| 32 | + return response.json() |
| 33 | + except requests.exceptions.RequestException as e: |
| 34 | + logging.error(f"Request for {n} failed: {e} (attempt {attempt + 1})") |
| 35 | + time.sleep(2**attempt) # Exponential backoff |
| 36 | + logging.error(f"Request for {n} failed after {MAX_RETRIES} attempts") |
| 37 | + return {} |
| 38 | + |
| 39 | + |
| 40 | +async def async_request(session: aiohttp.ClientSession, n: int) -> dict: |
| 41 | + """Asynchronous request with error handling, logging, and retry logic""" |
| 42 | + for attempt in range(MAX_RETRIES): |
| 43 | + try: |
| 44 | + response = await session.get( |
| 45 | + f"{BASE_URL}/{n}", headers=HEADERS, timeout=TIMEOUT |
| 46 | + ) |
| 47 | + response.raise_for_status() |
| 48 | + logging.info(f"Request returned for {n} with status code {response.status}") |
| 49 | + return await response.json() |
| 50 | + except (aiohttp.ClientError, asyncio.TimeoutError) as e: |
| 51 | + logging.error(f"Request for {n} failed: {e} (attempt {attempt + 1})") |
| 52 | + await asyncio.sleep(2**attempt) |
| 53 | + logging.error(f"Request for {n} failed after {MAX_RETRIES} attempts") |
| 54 | + return {} |
| 55 | + |
| 56 | + |
| 57 | +def sync_main(n: int) -> dict: |
| 58 | + with requests.Session() as session: |
| 59 | + start_time = time.time() |
| 60 | + results = [sync_request(session, i) for i in range(1, n + 1)] |
| 61 | + total_time = time.time() - start_time |
| 62 | + logging.info(f"Time taken for {n} requests: {total_time:.2f} seconds with sync") |
| 63 | + for result in results: |
| 64 | + logging.info(result) |
| 65 | + |
| 66 | + |
| 67 | +async def async_main(n: int) -> dict: |
| 68 | + async with aiohttp.ClientSession() as session: |
| 69 | + start_time = time.time() |
| 70 | + tasks = [async_request(session, i) for i in range(1, n + 1)] |
| 71 | + results = await asyncio.gather(*tasks) |
| 72 | + total_time = time.time() - start_time |
| 73 | + logging.info( |
| 74 | + f"Time taken for {n} requests: {total_time:.2f} seconds with async" |
| 75 | + ) |
| 76 | + for result in results: |
| 77 | + logging.info(result) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + n = 25 |
| 82 | + logging.info(f"Starting synchronous requests for {n} numbers") |
| 83 | + sync_main(n) |
| 84 | + |
| 85 | + logging.info(f"Starting asynchronous requests for {n} numbers") |
| 86 | + asyncio.run(async_main(n)) |
0 commit comments