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())