-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_request.py
47 lines (32 loc) · 1 KB
/
multi_request.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
from queue import Queue
from threading import Thread
import requests
def main():
UrlQ = Queue()
for x in xrange(1, 100):
UrlQ.add("http://www.reddit.com/")
UrlQ.add("http://www.nytimes.com/")
UrlQ.add("http://www.theatlantic.com/")
UrlQ.add("https://github.com/tpoll")
UrlQ.add("http://arstechnica.com/")
UrlQ.add("http://www.amazon.com/")
UrlQ.add("http://www.newyorker.com/")
RequestURls(UrlQ, Requester, 15)
def RequestURls(UrlQ, fun, NumberOfThreads=10):
threads = []
HtmlQ = Queue()
for x in xrange(0, NumberOfThreads):
threads.append(Thread(target=fun, args=(UrlQ, HtmlQ)))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
def Requester(UrlQ, HtmlQ):
while not UrlQ.is_empty():
try:
Url = UrlQ.remove()
HtmlQ.add(requests.get(Url))
except:
print "Request Failed: " + Url
if __name__ == '__main__':
main()