マルチスレッドの例は、 Python urllib2.urlopen() is slow, need a better way to read several urlsにありますが、「スレッドエラー」が発生するようですが、これが実際に何を意味するのかわかりません。私は時々、データをフェッチするように見えるんことがわかり、一方urllib2とスレッディング
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 505, in run
self.__target(*self.__args, **self.__kwargs)
File "demo.py", line 76, in read_url
print('Fetched %s from %s' % (len(data), url))
TypeError: object of type 'instancemethod' has no len()
、その後私は、次の2番目のエラーを取得:
urlList=[list of urls to be fetched]*100
def read_url(url, queue):
my_data=[]
try:
data = urllib2.urlopen(url,None,15).read()
print('Fetched %s from %s' % (len(data), url))
my_data.append(data)
queue.put(data)
except HTTPError, e:
data = urllib2.urlopen(url).read()
print('Fetched %s from %s' % (len(data), url))
my_data.append(data)
queue.put(data)
def fetch_parallel():
result = Queue.Queue()
threads = [threading.Thread(target=read_url, args = (url,result)) for url in urlList]
for t in threads:
t.start()
for t in threads:
t.join()
return result
res=[]
res=fetch_parallel()
reslist = []
while not res.empty: reslist.append(res.get())
print (reslist)
私は、次の最初のエラーを取得
をTraceback (most recent call last):
File "demo.py", line 89, in <module>
print str(res[0])
AttributeError: Queue instance has no attribute '__getitem__'
データをフェッチすると、結果がres []に表示されないのはなぜですか?御時間ありがとうございます。
更新状況は改善しているものの、read_url()関数の中で()読み読み取り変更した後(私は今、多くのページがフェッチ得る)が、それでもエラーました:
Exception in thread Thread-86:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 505, in run
self.__target(*self.__args, **self.__kwargs)
File "demo.py", line 75, in read_url
data = urllib2.urlopen(url).read()
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 397, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 510, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 429, in error
result = self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 605, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "/usr/lib/python2.7/urllib2.py", line 397, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 510, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 435, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 518, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 502: Bad Gateway
あなたのゲートウェイをチェックしましたか? – Arafangion
@Arafangion:私は502エラーで何ができるか分からなかった。私はそれを投稿した理由は、マルチスレッドで何かをしなければならないかどうか分からなかったからです。 – JohnJ