Pypprof HTTP Profiling Endpoints

0.0.1 · maintenance · verified Thu Apr 16

pypprof (version 0.0.1) adds HTTP-based endpoints to Python applications for collecting CPU and heap profiles, similar to Go's `net/http/pprof`. It leverages `zprofile` and `mprofile` under the hood for profile collection. The last release was in 2019, indicating the library is in a maintenance state rather than active development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `pypprof` into a simple Python application to expose profiling endpoints. After running, you can access CPU and heap profiles via `go tool pprof` (requires Go installed) or `curl` against the exposed `/debug/pprof/` endpoints. A background thread continuously performs a CPU-intensive task to generate data for profiling.

from pypprof.net_http import start_pprof_server
import time
import threading

def my_heavy_computation():
    total = 0
    for i in range(10000000):
        total += i * i
    return total

def background_task():
    while True:
        print(f"Performing heavy computation: {my_heavy_computation()}")
        time.sleep(1)

if __name__ == "__main__":
    # Start the pprof server on port 8081
    print("Starting pypprof server on port 8081...")
    start_pprof_server(port=8081)
    print("pypprof server started. Access profiles at http://localhost:8081/debug/pprof/")
    print("To fetch a CPU profile: go tool pprof -http=:8088 :8081/debug/pprof/profile")
    print("To fetch a heap profile: go tool pprof :8081/debug/pprof/heap")

    # Run a background task to generate some CPU load for profiling
    worker_thread = threading.Thread(target=background_task, daemon=True)
    worker_thread.start()

    try:
        while True:
            time.sleep(10) # Keep the main thread alive
    except KeyboardInterrupt:
        print("Exiting.")

view raw JSON →