URL을 여러 번 가져와야하는 크롤러를 작성 중이므로 모든 스레드를 동시에 (동시에) 실행하고 싶습니다.동시에 여러 스레드를 파이썬에서 실행하고 있습니까?
나는 그렇게해야하는 코드를 작성했습니다. 스레드가 동시에 실행되는 것처럼하지 않는 아파치 로그에서
import thread
from urllib2 import Request, urlopen, URLError, HTTPError
def getPAGE(FetchAddress):
attempts = 0
while attempts < 2:
req = Request(FetchAddress, None)
try:
response = urlopen(req, timeout = 8) #fetching the url
print "fetched url %s" % FetchAddress
except HTTPError, e:
print 'The server didn\'t do the request.'
print 'Error code: ', str(e.code) + " address: " + FetchAddress
time.sleep(4)
attempts += 1
except URLError, e:
print 'Failed to reach the server.'
print 'Reason: ', str(e.reason) + " address: " + FetchAddress
time.sleep(4)
attempts += 1
except Exception, e:
print 'Something bad happened in gatPAGE.'
print 'Reason: ', str(e.reason) + " address: " + FetchAddress
time.sleep(4)
attempts += 1
else:
try:
return response.read()
except:
"there was an error with response.read()"
return None
return None
url = ("http://www.domain.com",)
for i in range(1,50):
thread.start_new_thread(getPAGE, url)
는 거의 탐지의 요청 사이에 약간의 차이가있어 보인다하지만 난 스레드가 정말 평행하지 않은 것을 볼 수 있습니다.
나는 GIL에 대해 읽었으며, C \ C++ 코드를 호출하지 않고 건너 뛸 수있는 방법이 있습니까? GIL을 사용하여 스레딩을 수행하는 방법을 실제로 이해할 수 없습니까? 파이썬은 기본적으로 이전 스레드와 끝나는 즉시 다음 스레드를 해석합니까?
감사합니다.
에서 실행됩니다 것 urlopen하지 블록과 그 스레드가 병렬로 다운로드 의미? 서버를 소유하고 서버 측에서 인공 지연을 도입하여 3 초라고 말한 다음 스레드가 병렬로 시작하고 종료하는지 확인해야합니다. –