using python threading call rest endpoint in 20 threads
- User
using python threading call rest endpoint in 20 threads
- Assistant
To call a REST endpoint concurrently using Python threading, you can use the
threadingmodule along with therequestslibrary to make HTTP requests. Below is an example code snippet that demonstrates how to create 20 threads, each of which will call the specified REST endpoint. - User
using python threading call rest endpoint without arguments in 20 threads
- Assistant
Certainly! If you want to create 20 threads to call a REST endpoint without passing any arguments to the thread function, you can modify the previous example accordingly. Here's how you can do that:
import threading import requests # Define the URL of the REST endpoint you want to call url = 'https://api.example.com/your/endpoint' # Define a function that will be executed in each thread def call_api(): try: response = requests.get(url) print(f"Status Code: {response.status_code}, Response: {response.text}") except Exception as e: print(f"Exception occurred - {e}") # Create a list to hold the threads threads = [] # Create and start 20 threads for _ in range(20): thread = threading.Thread(target=call_api) threads.append(thread) thread.start() # Wait for all threads to complete for thread in threads: thread.join() print("All threads have completed their requests.")