-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchpool.py
More file actions
224 lines (185 loc) · 5.16 KB
/
fetchpool.py
File metadata and controls
224 lines (185 loc) · 5.16 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/python
from multiprocessing.pool import ThreadPool
import urllib2
from urllib2 import HTTPError
from urllib2 import URLError
class ContentLengthError (HTTPError):
pass
class HTTPFetchPool:
_num_thread = 5
_retry_thread = 50
_retry_limit = 10
_thread_pool = None
_retry_pool = None
_timeout = 3
_retry_timeout = 10
_retry_sleep = 3
def __init__ (self, num_thread=5, retry_thread=50, retry_limit=10):
self._num_thread = num_thread
self._retry_thread = retry_thread
self._retry_limit = retry_limit
def start (self):
self._thread_pool = ThreadPool(self._num_thread)
self._retry_pool = ThreadPool(self._retry_thread)
def addAsyncJob (self, url, headers=None, data=None, callback=None, *args,
**kwargs):
kwargs['_fetcher'] = self
kwargs['_url'] = url
kwargs['_header'] = headers
kwargs['_data'] = data
kwargs['_callback'] = callback
kwargs['_async'] = True
return self._thread_pool.apply_async(self.download, args, kwargs
, self.middleman)
def addSyncJob (self, url, headers=None, data=None, callback=None, *args,
**kwargs):
kwargs['_fetcher'] = self
kwargs['_url'] = url
kwargs['_header'] = headers
kwargs['_data'] = data
kwargs['_callback'] = callback
kwargs['_async'] = False
# try:
result = self._thread_pool.apply(self.download, args, kwargs)
# except Exception as err:
# result.status = -1
# result.exception = err
# print err.reason
# raise
return self.middleman(result)
def addRetryJob (self, *args, **kwargs):
if kwargs['_async']:
return self._retry_pool.apply_async(self.retry, args, kwargs, self.middleman)
else:
return self._retry_pool.apply(self.retry, args, kwargs)
@classmethod
def middleman (cls, result):
print "Middleman"
callback = result.kwargs['_callback']
if result.status == -1 and result.retry_asyncresult != None:
return result
if callback:
result = callback(result)
return result
def stop (self):
self._thread_pool.close()
self._thread_pool.join()
self._retry_pool.close()
self._retry_pool.join()
def retry (self, *args, **kwargs):
url = kwargs['_url']
headers = kwargs['_header']
data = kwargs['_data']
print "Start retry " + url
result = HTTPFetchResult()
retrycount = 0
while retrycount < self._retry_limit:
retrycount = retrycount + 1
try:
result = doDownload(url, headers, data, self._retry_timeout)
except (HTTPError, URLError) as e:
print "Error %d/%d" % (retrycount, self._retry_limit)
print e.reason
result.status = -1
result.exception = e
result.retry_asyncresult = None
except Exception as err:
print "Fatal Error " + url + " " + err.reason
result.status = -1
result.exception = err
result.retry_asyncresult = None
result.args = args
result.kwargs = kwargs
raise
# return result
else:
result.status = 0
result.exception = None
result.retry_asyncresult = None
break
time.sleep(_retry_sleep)
if result.status < 0:
print "Failed after %d Retries: %s" % (self._retry_limit, url)
raise
result.args = args
result.kwargs = kwargs
return result
def download (self, *args, **kwargs):
url = kwargs['_url']
headers = kwargs['_header']
data = kwargs['_data']
print "Start download " + url
result = HTTPFetchResult()
try:
result = doDownload(url, headers, data, self._timeout)
except Exception as err:
print "Moved to Retry Pool"
result.status = -1
result.exception = err
if kwargs['_async']:
result.retry_asyncresult = self.addRetryJob(*args, **kwargs)
else:
result = self.addRetryJob(*args, **kwargs)
result.args = args
result.kwargs = kwargs
return result
def doDownload (url, headers=None, data=None, timeout = 5):
result = HTTPFetchResult()
if data and headers:
req = urllib2.Request(url, data, headers)
elif headers:
req = urllib2.Request(url, headers=headers)
else:
req = urllib2.Request(url)
req_obj = None
#req = urllib2.Request(url)
req_obj = urllib2.urlopen(req, timeout=timeout)
result.status = 0
result.req_obj = req_obj
ret_data = req_obj.read()
result.data = ret_data
if "Content-Length" in req_obj.headers:
if len(ret_data) != int(req_obj.headers["Content-Length"]):
raise ContentLengthError()
else:
print "No CL"
return result
class HTTPFetchResult(object):
args = None
kwargs = None
data = None
status = 0
req_obj = None
exception = None
retry_asyncresult = None
#def test_callback(result):
# print result.args
# print "Completed"
#http_pool = HTTPFetchPool()
#http_pool.start()
#http_pool.addAsyncJob("http://www.google.com.hk", callback=test_callback)
#http_pool.addAsyncJob("http://www.google.com", callback=test_callback)
#http_pool.stop()
#class Argument(object):
# _args = []
# _kwargs = {}
#
# def __init__ (self, *args, **kwargs):
# self._args = args
# self._kwargs = kwargs
#def test (obj):
# print obj._kwargs['name']
# print obj._args[0]
# return
#def th (*args, **kwargs):
# print "hihi"
# tempobj = Argument()
# tempobj._args = args
# tempobj._kwargs = kwargs;
# return tempobj
#threadpool = ThreadPool(5)
#temp = threadpool.apply_async(th, (345,), {'name':"Sashiko"},test)
#obj = temp.get()
#print obj._args[0]
#threadpool.close()
#threadpool.join()