Files
live-campus-mcs-p-2027.2/Semaine_09/Asyncio_examples/exo_04.py
T
gauvainboiche ce1f0e513a feat: Semaine 9
2026-05-15 16:24:56 +02:00

49 lines
1.6 KiB
Python

URLS = [
"https://httpbin.org/get",
"https://httpbin.org/ip",
"https://httpbin.org/user-agent",
"https://httpbin.org/headers",
"https://httpbin.org/uuid",
"https://httpbin.org/base64/SWYgeW91IGNhbiByZWFkIHRoaXMsIGJyYXZvICE=",
]
import asyncio, random, time
async def producer(queue: asyncio.Queue, urls):
async def fetch_url(url):
delay = random.uniform(0.5, 2.0)
print(f"Producer : récupération de l'URL {url=}")
await asyncio.sleep(delay)
url = {"id": url, "name": f"{url}"}
print(f"Producer : récupération de l'URL avec l'ID {url=} - delay {delay:.2f}s.")
await queue.put(url)
await asyncio.gather(*(fetch_url(id) for id in urls))
for _ in range(len(urls)):
await queue.put(None)
async def consumer(queue):
while True:
url = await queue.get()
if url is None:
break
delay = random.uniform(0.5, 2.0)
print(f"Consumer : récupération des posts pour {url["name"]}")
await asyncio.sleep(delay)
posts = [f"Post {i} par l'URL : {url["name"]}" for i in range(1, 3)]
print(f"Consumer : récupération de {len(posts)} posts pour {url["name"]} - delay {delay:.2f}s.")
for post in posts:
print(f"--- {post} ---")
async def main():
queue = asyncio.Queue()
start = time.perf_counter()
await asyncio.gather(
producer(queue, URLS),
*(consumer(queue) for _ in URLS)
)
print(f"Temps total d'exécution : {time.perf_counter() - start:.2f}s")
if __name__ == "__main__":
random.seed(444)
asyncio.run(main())