2013-03-04 93 views
0

使用gevent(1.0b4)下載html文件時,我想使用進度條顯示進度。
我寫了下面的代碼,但代碼總是存在一些錯誤。我希望有人能幫幫忙!Progress Bar and 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) 

所以,你不能傳遞更多然後2個參數。

更新: 如果我理解你的權利,那麼你可以使用這樣的事情:

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

我是新來GEVENT,可能你只是根據你的更正修改我的代碼,並再次後呢?即使link_exception()只需要2個參數(移除上面的參數'file_path'),將顯示另一個錯誤消息:TypeError:'str'對象不可調用。 – 2013-03-04 14:34:57

+0

@Matt Elson,見更新 – 2013-03-05 13:28:11

相關問題