5

我有一个非常简单的 Python 脚本,使用 gevent.pool 下载 URL(见下文)。该脚本运行了几天,然后锁定。我注意到当时内存使用率非常高。我是否错误地使用了 gevent?

import sys

from gevent import monkey
monkey.patch_all()
import urllib2

from gevent.pool import Pool

inputFile = open(sys.argv[1], 'r')
urls = []
counter = 0
for line in inputFile:
    counter += 1
    urls.append(line.strip())
inputFile.close()

outputDirectory = sys.argv[2]

def fetch(url):
    try:
        body = urllib2.urlopen("http://" + url, None, 5).read()
        if len(body) > 0:
            outputFile = open(outputDirectory + "/" + url, 'w')
            outputFile.write(body)
            outputFile.close()
            print "Success", url
    except:
        pass

pool = Pool(int(sys.argv[3]))
pool.map(fetch, urls)
4

1 回答 1

2
        body = urllib2.urlopen("http://" + url, None, 5).read()

上面的行将内存中的全部内容作为字符串读取。为防止这种情况,请按如下方式更改 fetch():

def fetch(url):
    try:
        u = urllib2.urlopen("http://" + url, None, 5)
        try:
            with open(outputDirectory + "/" + url, 'w') as outputFile:
                while True:
                    chunk = u.read(65536)
                    if not chunk:
                        break
                    outputFile.write(chunk)
        finally:
            u.close()
        print "Success", url
    except:
        print "Fail", url
于 2013-06-12T10:28:16.903 回答