-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl_threading.py
More file actions
35 lines (28 loc) · 899 Bytes
/
Copy pathurl_threading.py
File metadata and controls
35 lines (28 loc) · 899 Bytes
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
import threading
import requests
from concurrent.futures import ThreadPoolExecutor
def get_urls():
with open("urls.txt") as file:
urls = [line.strip() for line in file.readlines() if line.strip() ]
return urls
def hit_url(url):
try:
response = requests.get(url)
print(response)
except requests.exceptions.RequestException as e:
print(f"url--{url}---Error{e}")
def execute(urls):
threads = []
for url in urls:
threads.append(threading.Thread(target=hit_url, args = (url,)))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
def execute_with_thread_pool(urls, max_thread=5):
with ThreadPoolExecutor(max_workers=max_thread) as exector:
exector.map(hit_url, urls)
if __name__ == "__main__":
urls = get_urls()
#execute(urls)
execute_with_thread_pool(urls)