Skip to content

Instantly share code, notes, and snippets.

@eduardogpg
Last active November 6, 2024 21:47
Show Gist options
  • Save eduardogpg/946601b578d3bbdc968ae4fe6ba77206 to your computer and use it in GitHub Desktop.
Save eduardogpg/946601b578d3bbdc968ae4fe6ba77206 to your computer and use it in GitHub Desktop.
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