#CODE en python 3.10.0 64 bits (VScode 1.62.3)
import datetime
import asyncio
import aiohttp
import requests
# ----------------------------------------------------------------------------------------------------------------------------
def req_bloquante(num):
print(f"Get {num}")
uid = requests.get("https://httpbin.org/uuid").json()["uuid"]
print(f"Res {num}: {uid}")
def faire_toutes_les_requetes():
for x in range(51):
req_bloquante(x)
print("=====> ### Bloquant : ")
start = datetime.datetime.now()
faire_toutes_les_requetes()
exec_time = (datetime.datetime.now() - start).seconds
print(f"Downloading all take {exec_time} seconds\n")
# ----------------------------------------------------------------------------------------------------------------------------
async def requeteSansBloquer(semaphore, num, session):
print(f"Get {num}")
# await semaphore.acquire() # putting this await semaphore commande the Res is ordering but process very slow ~60 sec
async with session.get("https://httpbin.org/uuid") as response:
uid = (await response.json())["uuid"]
# await asyncio.sleep(delay=3) #delay = number of seconds
# semaphore.release()
print(f"Res {num}: {uid}")
# return uid # ?
async def main():
semaphore = asyncio.Semaphore(value=1)
tasks = []
async with aiohttp.ClientSession(
loop=loop, connector=aiohttp.TCPConnector(ssl=False)
) as session:
for x in range(51):
tasks.append(requeteSansBloquer(semaphore, x, session))
await asyncio.gather(*tasks)
print("This is after the loop...")
if __name__ == "__main__":
print("=====> ### Non Bloquant : ")
start = datetime.datetime.now()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
exec_time = (datetime.datetime.now() - start).microseconds
print(f"Downloading all take {exec_time} micro seconds\n")