2013-03-04 3 views
0

gevent (1.0b4)를 사용하여 html 파일을 다운로드 할 때 진행률 표시 줄을 사용하여 진행률을 표시하려고합니다.
아래 코드를 작성했지만 코드에 항상 오류가 있습니다. 누군가가 도울 수 있기를 바랍니다.진행 표시 줄 및 gevent

file_path='temp' 
url_count=len(urls) 

def progress_bar(file_path, file_count): # 
    file_count = long(file_count) 
    width = 32 
    last_count = 0 
    try: 
     while True: 
      if os.path.isdir(file_path): 
       current_count = len(glob.glob1(myPath,"*.html")) 
       percentage = current_count*100/file_count 
       current_width = width*percentage/100 
       sys.stderr.write('% 3d%% [%s%s] %s/s \r' % (percentage, '#'*current_width, ' '*(width-current_width), current_count - last_count)) 
       last_count = current_count 
      time.sleep(1)  
    except: 
     sys.stderr.write('100%% [%s]\n' % ('#'*width)) 

def print_head(url):  
    data = urllib2.urlopen(url) 
    htmlFile = open(file_path+'/'+url+'.html', 'w') 
    htmlFile.write(data.read()) 
    htmlFile.close()  
    raise Exception("done!")  

jobs = [gevent.spawn(print_head, url) for url in urls] 
x = [g.link_exception(progress_bar,file_path,url_count) for g in jobs] 
gevent.joinall(jobs) 

역 추적

Traceback (most recent call last): 
     File "E:\tt\test.py", line 39, in <module> 
     x = [g.link_exception(progress_bar,file_path,url_count) for g in jobs] # 
    TypeError: link_exception() takes at most 3 arguments (4 given) 

답변

1

대답은 귀하의 질문입니다! 이 방법의 정의를 살펴 경우

link_exception() takes at most 3 arguments (4 given) 

은 다음과 같이 표시됩니다

def link_exception(self, callback, SpawnedLink=FailureSpawnedLink): 
     """Like :meth:`link` but *callback* is only notified when the greenlet dies because of unhandled exception""" 
     self.link(callback, SpawnedLink=SpawnedLink) 

그래서 당신이 더 많은 그런 다음이 개 매개 변수를 전달할 수 없습니다. 당신은 당신의 수정에 따라 내 코드를 수정하고 다시 게시 할 수있는, 내가 gevent에 새로운 오전

file_path = os.path.abspath(os.path.dirname(__file__)) 
    url_count = len(urls) 

    def progress_bar(green): 
     width = 32 
     current_count = getattr(progress_bar, 'current_count', 0) + 1 
     percentage = current_count * 100/url_count 
     current_width = width * percentage/100 
     print('% 3d%% [%s%s] %s/s \r' % (percentage, '#' * current_width, ' ' * (width - current_width), current_count)) 
     setattr(progress_bar, 'current_count', current_count) 

     url, exc = green.value 
     if exc: 
      print 'Download {} failed with error {}'.format(url, exc) 
     else: 
      print 'Download {} success'.format(url) 

    def print_head(url): 
     exc = None 
     try: 
      data = urllib2.urlopen(url) 
      htmlFile = open(''.join([file_path, '/', clearFileName(url), '.html']), 'wb+') 
      htmlFile.write(data.read()) 
      htmlFile.close() 
     except Exception, ex: 
      exc = ex 
     return url,exc 

    def clearFileName(url): 
     return url.replace('/', '_').replace(':', '_').replace('.', '_').replace('#', '_').replace('=', '_') 

    jobs = [gevent.spawn(print_head, url) for url in urls] 
    [g.link(progress_bar) for g in jobs] 
    gevent.joinall(jobs) 
+0

:

업데이트 : 난 당신이 뭔가를 사용할 수있는 바로 그때 당신을 이해한다면 ? link_exception()도 인자를 두 개 취합니다 (위에서 'file_path'인수를 제거함). 다른 오류 메시지가 표시됩니다. TypeError : 'str'객체를 호출 할 수 없습니다. –

+0

@Matt Elson, 업데이트보기 –