0

在使用 gevent(1.0b4) 下载 html 文件时,我想使用进度条来显示进度。
我在下面写了代码,但是代码总是存在一些错误。我希望有人能帮帮忙!

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)
4

1 回答 1

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)
于 2013-03-04T13:37:35.183 回答