我想診斷爲什麼我的Python服務器應用程序泄漏內存。該應用程序接受圖片的請求url使用Vips調整其大小並返回圖片。在每次請求之後,內存使用量會隨着原始圖像的大小而大致增長。Python服務器應用程序泄漏內存
from fapws import base
import fapws._evwsgi as evwsgi
from gi.repository import Vips
import urllib2
import hmac
import hashlib
import base64
import StringIO
from boto.s3.connection import S3Connection
from boto.s3.bucket import Bucket
def start():
evwsgi.start('0.0.0.0', '80')
evwsgi.set_base_module(base)
def lfrThumbnail(environ, start_response):
try:
parameters = environ['PATH_INFO'].split('/')
s3File = 'my s3 url' + parameters[0]
width = float(parameters[1])
height = float(parameters[2])
hmacSignatureUser = parameters[3]
hmacSignature = some hasing code...
if not (hmacSignatureUser == hmacSignature):
print hmacSignatureUser
print hmacSignature
print hmacSignatureUser == hmacSignature
raise Exception
bufferedImage = urllib2.urlopen(s3File).read()
image = Vips.Image.new_from_buffer(bufferedImage, '')
imageWidth = float(image.width)
imageHeight = float(image.height)
imageAspectRatio = imageWidth/imageHeight
if (width > imageWidth) or (height > imageHeight):
image = image
elif abs((imageAspectRatio/(width/height)) - 1) < 0.05:
image = image.resize(width/imageWidth)
else:
scaleRatioWidth = width/imageWidth
scaleRatioHeight = height/imageHeight
maxScale = max(scaleRatioWidth, scaleRatioHeight)
image = image.resize(maxScale)
cropStartX = (image.width - width)/2
cropStartY = (image.height - height)/2
image = image.crop(cropStartX, cropStartY, width, height)
except Exception, e:
start_response('500 INTERNAL SERVER ERROR', [('Content-Type','text')])
return ['Error generating thumbnail']
start_response('200 OK', [
('Content-Type','image/jpeg'),
('Cache-Control: max-stale', '31536000')
])
return [image.write_to_buffer('.jpg[Q=90]')]
evwsgi.wsgi_cb(('/lfr/', lfrThumbnail))
evwsgi.set_debug(0)
evwsgi.run()
if __name__ == '__main__':
start()
我使用muppy,該pympler跟蹤但是每個DIFF嘗試後的圖像打開/關閉操作顯示正在使用的只有幾個字節。
外部C庫可能導致內存泄漏嗎?如果是這樣,那麼如何調試呢。
如果它是什麼我跑碼頭工人,容器
使用Vips.leak_set(真),貴賓只報告當服務器停止峯值內存使用量,這也是Python腳本退出時。我應該在處理請求後關閉vip嗎? – Nicholas
我會讓vips運行,您將節省啓動/關閉時間。峯值內存報告對於測試非常方便,在生產中不是非常有用。 C API有一個能夠獲得當前高峯內存的東西,但它不會暴露給Python,我會添加它。libvips有一個相當大的測試套件,全部用Python編寫,其中一個測試是「整個測試套件是否完整,沒有內存泄漏?」,所以希望你不應該在服務器中看到泄漏,而且更多的是再檢查一遍。 – user894763