18 lines
No EOL
608 B
Python
18 lines
No EOL
608 B
Python
import asyncio
|
|
from datetime import datetime
|
|
import random
|
|
|
|
|
|
async def fetch(url: str, sem: asyncio.Semaphore) -> None:
|
|
async with sem:
|
|
print(f"Fetching of {url} started at {datetime.today()}")
|
|
await asyncio.sleep(round(random.random() + 0.5, 2))
|
|
print(f"Fetching of {url} ended at {datetime.today()}")
|
|
|
|
async def main():
|
|
semaphor = asyncio.Semaphore(3)
|
|
tasks: list[str] = [fetch(url, semaphor) for url in [f"https://jsonplaceholder.typicode.com/posts/{n}" for n in range(1, 11)]]
|
|
return await asyncio.gather(*tasks)
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main()) |