-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimultaneous_requests.py
40 lines (31 loc) · 1.01 KB
/
simultaneous_requests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# import requests
# import threading
# def send_request():
# url = "http://127.0.0.1:8888/css/style.css"
# response = requests.get(url)
# print(response.status_code, response.headers)
# num_requests = 10
# threads = []
# for _ in range(num_requests):
# t = threading.Thread(target=send_request)
# threads.append(t)
# t.start()
# for t in threads:
# t.join()
import requests
import concurrent.futures
def make_request(url):
try:
response = requests.get(url)
print(f"Response from {url}: {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"Error while fetching {url}: {e}")
urls = [
'http://127.0.0.1:8888/css/style.css',
'http://127.0.0.1:8888/css/style.css',
'http://127.0.0.1:8888/css/style.css'
]
MAX_CONCURRENT_REQUESTS = 5
with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_CONCURRENT_REQUESTS) as executor:
futures = [executor.submit(make_request, url) for url in urls]
concurrent.futures.wait(futures)