2013-07-03 5 views
0

나는 Discogs API를 파이썬과 함께 사용하는 짧은 프로그램을 작성했지만 실제 웹 응용 프로그램에는 사용할 수없는 속도가 느립니다. 비단뱀의 결과가 프로파일 여기에httplib (discogs API)의 python 성능

# -*- coding: utf-8 -*- 

import profile 
import discogs_client as discogs 

def main(): 
    discogs.user_agent = 'Mozilla/5.0' 
    #dump released albums into the file. You could also print it to the console 
    f=open('DiscogsTestResult.txt', 'w+') 

    #Use another band if you like, 
    #but if you decide to take "beatles" you will wait an hour! (cause of the num of releases) 
    artist = discogs.Artist('Faust') 
    print >> f, artist 
    print >> f," " 

    artistReleases = artist.releases 
    for r in artistReleases: 
     print >> f, r.data 
     print >> f,"---------------------------------------------" 


print 'Performance Analysis of Discogs API' 
print '=' * 80 
profile.run('print main(); print') 

과 :

Performance Analysis of Discogs API 
================================================================================ 
    82807 function calls (282219 primitive calls) in 177.544 seconds 
    Ordered by: standard name 
    ncalls tottime percall cumtime percall filename:lineno(function) 
     188 121.013 0.644 121.013 0.644 :0(connect) 
     206 52.080 0.253 52.080 0.253 :0(recv) 
     1 0.036 0.036 177.494 177.494 <string>:1(<module>) 
     188 0.013 0.000 175.234 0.932 adapters.py:261(send) 
     376 0.005 0.000 0.083 0.000 adapters.py:94(init_poolmanager) 
     188 0.008 0.000 176.569 0.939 api.py:17(request) 
     188 0.007 0.000 176.577 0.939 api.py:47(get) 
     188 0.015 0.000 173.922 0.925 connectionpool.py:268(_make_request) 
     188 0.015 0.000 174.034 0.926 connectionpool.py:332(urlopen) 
     1 0.496 0.496 177.457 177.457 discogsTestFullDump.py:6(main) 
     564 0.009 0.000 176.613 0.313 discogs_client.py:66(_response) 
     188 0.012 0.000 176.955 0.941 discogs_client.py:83(data) 
     188 0.011 0.000 51.759 0.275 httplib.py:363(_read_status) 
     188 0.017 0.000 52.520 0.279 httplib.py:400(begin) 
     188 0.003 0.000 121.198 0.645 httplib.py:754(connect) 
     188 0.007 0.000 121.270 0.645 httplib.py:772(send) 
     188 0.005 0.000 121.276 0.645 httplib.py:799(_send_output) 
     188 0.003 0.000 121.279 0.645 httplib.py:941(endheaders) 
     188 0.003 0.000 121.348 0.645 httplib.py:956(request) 
     188 0.016 0.000 121.345 0.645 httplib.py:977(_send_request) 
     188 0.009 0.000 52.541 0.279 httplib.py:994(getresponse) 
     1 0.000 0.000 177.544 177.544 profile:0(print main(); print) 
     188 0.032 0.000 176.322 0.938 sessions.py:225(request) 
     188 0.030 0.000 175.513 0.934 sessions.py:408(send) 
     752 0.015 0.000 121.088 0.161 socket.py:223(meth) 
    2256 0.224 0.000 52.127 0.023 socket.py:406(readline) 
     188 0.009 0.000 121.195 0.645 socket.py:537(create_connection) 

는 사람이 어떻게 어떤 생각을 가지고 있습니까 여기 파이썬 코드와 파이썬 프로파일 결과 (게시 시간 만 점을 소모을)입니다 이 속도를 높여 라. discogs_client.py에서 변경 사항이 더 빨라지기를 바랍니다. httplib에서 다른 것 또는 기타로 변경되었을 수 있습니다. 아니면 http 대신 다른 프로토콜을 사용하는 것이 더 빠릅니까?

은 (discogs_client.py의 소스는 여기에 액세스 할 수 있습니다 : "https://github.com/discogs/discogs_client/blob/master/discogs_client.py")

사람이 어떤 생각을 가지고 있다면 많은 사람들이 혜택을 누릴 것이라고 응답하시기 바랍니다. 감사합니다 다니엘

답변

1

UPDATE : Requests are throttled by the server to one per second per IP address. Your application should (but doesnt have to) take this into account and throttle requests locally, too.

병목 개별 자료를 검색하는의 (에 Discogs) 서버 끝에 것으로 보인다 다음에 Discogs 문서에서. 더 빠른 서버에 돈을주는 것 외에는 실제로 할 수있는 일이 없습니다.

내 제안은 결과를 캐시하는 것일뿐입니다. 아마도 도움이되는 유일한 방법 일 것입니다. 다음과 같이 discogs.APIBase._response를 다시 작성 :

def _response(self): 
    if not self._cached_response: 
     self._cached_response=self._load_response_from_disk() 
    if not self._cached_response: 
     if not self._check_user_agent(): 
      raise UserAgentError("Invalid or no User-Agent set.") 
     self._cached_response = requests.get(self._uri, params=self._params, headers=self._headers) 
     self._save_response_to_disk() 

    return self._cached_response 

다른 방법은 로그에 요청을 작성하고 로그를 읽을 다음, 다른 프로세스에서, "우리가, 나중에 다시 시도해 모른다"라고하는 것입니다, 데이터를 다운로드하여 데이터베이스에 저장하십시오. 그런 다음 나중에 다시 돌아 오면 요청한 데이터가 준비됩니다.

_load_response_from_disk() 및 _save_response_to_disk()를 직접 작성해야합니다. 저장된 데이터의 키는 _uri, _params, and _headers이어야하며 데이터에 타임 스탬프가 포함되어야합니다. 데이터가 너무 오래되면 (상황에 따라, 나는 수개월 단위로 제안 할 것입니다. 번호 붙이기가 영구적인지 알 수 없습니다. 처음에는 며칠이나 걸릴 것입니다), 발견되지 않으면 반환하지 않습니다. 스토리지는 동시 액세스와 빠른 인덱스 (아마도 데이터베이스)를 처리해야합니다.

+0

Didnt는 "IP 주소 당 초당 하나"를 알고 있습니다. 고맙습니다! 프리 패치를하는 것이 좋겠지 만 웹 사이트를 파싱하는 것이 더 빠를 수도 있습니다. 어쨌든 고맙습니다. – user1911091

0

이 시도 대신 >> 인쇄를 사용하여 파일을 사용 f.write ('안녕하세요 \ n을')에 기록.

+0

오버 헤드는 레코드 당 하나씩 http gets에 있습니다. 출력은 아무것도 계산하지 않습니다. –