Skip to main content

Multiprocessing compatible memory leak debugger inspired by dozer/dowser

Project description

PyLoot

PyLoot is a memory leak detector based on Dozer and vprof with added support for process based server workloads.

This project is in active development and may contain bugs or otherwise work in ways not expected or intended.

Installation

$ pip install pyloot

Usage

from pyloot import PyLoot
loot = PyLoot()


"""
Collect objects still in `gc.get_objects` after a call to `gc.collect`.
"""
loot.collect_objects()

"""
Collecting data in a background thread every 30 seconds.

If gevent is detected, gevent.threadpool.spawn is used.
Otherwise, threading.Thread is used.
"""
loot.start()

"""
Stop running the collector background thread.

NOTE: This does not do a "final" collection.
To ensure objects were collected in a short lived execution, call collect_objects().

:param blocking: When true, wait until the thread has died
"""
loot.stop()

"""
Return a WSGI compatible application serving the PyLoot remote backend and
and the website.
:return: ::class::`PyLootServer`
"""
loot.get_wsgi()

Running embedded

Starlette/FastApi/ASGI

from pyloot import PyLoot
from fastapi import FastAPI
from starlette.applications import Starlette
from starlette.middleware.wsgi import WSGIMiddleware

app = FastAPI()  # or Starlette()

pyloot = PyLoot()
app.on_event("startup")(pyloot.start)
app.mount("/_pyloot", WSGIMiddleware(pyloot.get_wsgi()))

Flask/WSGI

from pyloot import PyLoot
from flask import Flask
from werkzeug.middleware.dispatcher import DispatcherMiddleware

app = Flask(__name__)

pyloot = PyLoot()
app.on_before_first_request(pyloot.start)

app = DispatcherMiddleware(app, {
    '/_pyloot': pyloot.get_wsgi()
})

Running in remote mode (multi-process servers)

# Embedded code
from pyloot import PyLoot
...
pyloot = PyLoot(host="127.0.0.1", port=8000)
...
# Start the remote server
$ pyloot --help
usage: pyloot [-h HOST] [-p PORT] [--help]

optional arguments:
-h HOST, --host HOST  Host to listen on. (Default: 0.0.0.0)
-p PORT, --port PORT  Port to listen on. (Default: 8000)
--help                show this help message and exit

Bypass the multiprocessing check

If pyloot detects it is running in a multiprocessing environment with an inmemory backend it will refuse to serve the webpages/requests.

This is common for gunicorn servers running with multiple workers. If you run pyloot embedded in a gunicorn server with multiple workers statistics will be collected in each individual worker and a random worker will be selected when returning statistics. This can still give an idea of what is going on but is considered in accurate.

The WSGIMiddleware of starlette sets environ["wsgi.multiprocess"]=True regardless of the server. This can by bypassed with a wrapper use with caution:

pyloot = PyLoot()

def pyloot_wrapper(wsgi_environ, start_response):
    pyloot_environ = wsgi_environ.copy()
    pyloot_environ["wsgi.multiprocess"] = False
    wsgi = pyloot.get_wsgi()
    return wsgi(pyloot_environ, start_response)

app.mount("/_pyloot", WSGIMiddleware(pyloot_wrapper))

Disabling gzip encoding

By default, the pyloot server will gzip encode the response metadata. If pyloot is running behind a middleware that gzip encodes data the encoding could happen twice. This will result in the following error being shown in the UI:

Error parsing the response data. Check the server logs. If everything looks ok, you make need to disable gzip in pyloot. For more info see the README.

To disable gzip encoding do the following:

from pyloot import PyLoot
from pyloot import PyLootServer

pyloot = PyLoot(server=PyLootServer(disable_response_gzip=True))

If a remote server is used, it must be configured directly on the server like so:

from pyloot import PyLoot
from pyloot import PyLootServer
from pyloot import HTTPRemoteBackend

backend = HTTPRemoteBackend(host="127.0.0.1", port=8000)
pyloot = PyLoot(server=PyLootServer(backend=backend, disable_response_gzip=True))

Screenshots

View history of object counts by object group:

history screenshot

Modify history page size

history screenshot

Search history page

history screenshot

View objects by group

objects by group

Modify objects fetch size

objects by group

Modify objects page size

objects by group

View an object, its attributes, repr, children, and parents

view and object

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyloot-0.0.6.tar.gz (184.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyloot-0.0.6-py3-none-any.whl (187.4 kB view details)

Uploaded Python 3

File details

Details for the file pyloot-0.0.6.tar.gz.

File metadata

  • Download URL: pyloot-0.0.6.tar.gz
  • Upload date:
  • Size: 184.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.13

File hashes

Hashes for pyloot-0.0.6.tar.gz
Algorithm Hash digest
SHA256 5c4e213132f6e59f43de07419f2886ee71ea099649c23cde38b0a1819a8929cf
MD5 6e01562f4671442d15d47152832bdf30
BLAKE2b-256 9d1e81c7e01ff0247e22c4ed1ab4852214baeba93083b2b82834ce58d4dc47fc

See more details on using hashes here.

File details

Details for the file pyloot-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: pyloot-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 187.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.13

File hashes

Hashes for pyloot-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 38d907433f41f1ee029b8a55185cf3e8194cae92c253b91a79421c466f4932ca
MD5 262dbc5a05c2b30442ca87d2f092d96d
BLAKE2b-256 caa8c48ae6ba6892345d0dbe5ce511669c5002f9f06aaaca25af07432ebfd877

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page