I'm using WSGI Daemon process for my Python application, I'm having a problem trying to get the memory usage of each request using the resource
module.
It will work as I want it to if using CGI, but with WSGI, the memory usage count gets shared between requests. I believe this has something to do with threading as WSGI spawns processes & threads unlike when using CGI which just spawns a new process for each request.
Here is a minimal example of a WSGI Python application to get peak memory usage of example.com/a
and example.com/b
:
def application(environ, start_response):
if environ['REQUEST_URI'] == '/a':
pass
elif environ['REQUEST_URI'] == '/b':
# Use lots of memory
x = ''
for _i in range(0, 30000):
x += ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaqaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
'aaaaaaaaaaaaawaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
# Show peak memory used
# https://stackoverflow.com/questions/938733/total-memory-used-by-python-process#answer-7669482
import resource
mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
content = str(mem)
status = '200 OK'
header = [('Content-type', 'text/html; charset=utf-8')]
start_response(status, header)
content = content.encode('utf-8')
return [content]
If I visit example.com/a
the memory usage will show ~30008
If I visit example.com/b
the memory usage will show ~47984
But if I visit example.com/a
again (after visiting example.com/b
), it will show ~47984
instead of ~3008
.
What I want to happen for the above code is:
example.com/a
to always show ~30008
example.com/b
to always show ~47984
Any help is much appreciated.