Last active
December 4, 2024 18:01
-
-
Save dansbecker/e98dc3acc94026e06d562c6f77f39b3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import aiohttp | |
# Example async function that simulates making an API call | |
async def make_api_call(semaphore, session, task_id): | |
async with semaphore: # Ensure we respect the semaphore limit | |
print(f"Task {task_id}: Starting API call...") | |
try: | |
async with session.get(f"https://httpbin.org/get?task={task_id}") as response: | |
result = await response.json() | |
print(f"Task {task_id}: Completed with response {result['args']}") | |
return result['args'] | |
except Exception as e: | |
print(f"Task {task_id}: Failed with error {e}") | |
return None | |
def main(): | |
num_tasks = 50 | |
semaphore_limit = 30 | |
semaphore = asyncio.Semaphore(semaphore_limit) | |
async def run_tasks(): | |
async with aiohttp.ClientSession() as session: | |
tasks = [make_api_call(semaphore, session, task_id) for task_id in range(1, num_tasks + 1)] | |
results = await asyncio.gather(*tasks) | |
return results | |
# Run the event loop and get the results | |
results = asyncio.run(run_tasks()) | |
print("All tasks completed.") | |
print("Results:", results) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment