-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreading.py
60 lines (46 loc) · 1.39 KB
/
threading.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import requests
import os
import threading
import time
from collections import deque
def crawl(index, queue):
'''
crawl urls in the queue
'''
while len(queue) != 0:
url = queue.popleft()
try:
r = requests.get(url, timeout=10)
print(len(queue), 'Thread', index, r.status_code, url)
except Exception as e:
print(len(queue), 'Thread', index, url, 'Error:', e)
# main
if __name__ == "__main__":
# get websites file path
script_dir = os.path.dirname(__file__)
filename = 'alexa.txt'
file_path = os.path.join(script_dir, filename)
# initialize url queue
url_queue = deque()
with open(file_path, 'r') as f:
lines = f.readlines()
for line in lines:
url = line.split('\t')[1].replace('\n', '')
url_queue.append(url)
# initialize thread list
thread_list = []
for i in range(1, 5):
thread = threading.Thread(target=crawl, args=(i, url_queue))
thread_list.append(thread)
# record start time
start = time.time()
# start threads
for thread in thread_list:
thread.start()
# wait for threads finish
for thread in thread_list:
thread.join()
# record end time
end = time.time()
print('Total time is: {}'.format(end - start))
print('Done')