Last active
November 6, 2024 21:47
-
-
Save eduardogpg/946601b578d3bbdc968ae4fe6ba77206 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 threading | |
import requests | |
import time | |
def fetch_data(post_id): | |
url = f"https://jsonplaceholder.typicode.com/posts/{post_id}" | |
response = requests.get(url) | |
if response.status_code == 200: | |
print(f"Successfully fetched data for post {post_id}") | |
def with_threads(): | |
threads = [] | |
for post_id in range(1, 50): | |
thread = threading.Thread(target=fetch_data, args=(post_id,)) | |
threads.append(thread) | |
thread.start() | |
for thread in threads: | |
thread.join() | |
if __name__ == '__main__': | |
start = time.time() | |
# with_threads() | |
for post_id in range(1, 50): | |
fetch_data(post_id) | |
print("All requests completed.", time.time() - start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment