Enable external profiler attachment for Python processes
Project description
profile-me
Enable external profiler attachment for Python processes on Linux.
The Problem
Modern Linux distributions use Yama LSM to restrict which processes can use ptrace. By default (ptrace_scope=1), only parent processes can attach to their children.
This means tools like py-spy, Yantra, gdb, and other profilers/debuggers cannot attach to your Python process unless you either:
- Run as root
- Change system-wide ptrace settings
- Have the target process explicitly opt-in
The Solution
profile-me provides a simple one-liner to opt your process in:
import profile_me
profile_me.enable_profiling()
This calls prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) which tells the kernel "allow any process owned by my user to attach to me."
Installation
pip install profile-me
Usage
Basic Usage
Add this near the top of your script:
import profile_me
profile_me.enable_profiling()
# Your code here...
One-liner (no import needed at top)
__import__('profile_me').enable_profiling()
Environment Variable
Set PROFILE_ME=1 to auto-enable when the module is imported:
PROFILE_ME=1 python my_script.py
Add PROFILE_ME_QUIET=1 to suppress the message.
Context Manager
import profile_me
with profile_me.profiling_enabled():
# Code you want to profile
run_computation()
Check Status
import profile_me
profile_me.enable_profiling()
print(f"Enabled: {profile_me.is_profiling_enabled()}")
print(f"PID: {profile_me.get_pid()}")
Example with py-spy
# Terminal 1
PROFILE_ME=1 python my_server.py
# Terminal 2
py-spy record -p <PID> -o profile.svg
API Reference
enable_profiling(quiet=False) -> bool
Enable external profiler attachment. Returns True on success.
quiet: IfTrue, suppress the informational message to stderr.
is_profiling_enabled() -> bool
Check if profiling has been enabled for this process.
get_pid() -> int
Get the current process ID (convenience wrapper around os.getpid()).
profiling_enabled(quiet=False)
Context manager that calls enable_profiling() on entry.
Platform Support
- Linux: Full support
- macOS: No-op (macOS doesn't have Yama)
- Windows: No-op (Windows doesn't have Yama)
Security Considerations
Calling enable_profiling() allows any process running as your user to attach to your Python process. This is the same permission model as running with ptrace_scope=0.
Only use this in development/debugging scenarios or when you trust all processes running under your user account.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file profile_me-0.1.0.tar.gz.
File metadata
- Download URL: profile_me-0.1.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7828e2b4456d8ff421866104328e697d7ae8e44696befd4572e0c3edd05f38e5
|
|
| MD5 |
764f31ec992b82689f24748e03031474
|
|
| BLAKE2b-256 |
8a75ce687ef288764ff7e5cb0bd2f93b7204f9c346904f96232b07e53ce7ede0
|
File details
Details for the file profile_me-0.1.0-py3-none-any.whl.
File metadata
- Download URL: profile_me-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23fe84c9d1c691fed5f6302e703a3b86f9d2eea99496e64678b352639d2c30e6
|
|
| MD5 |
6f3aa1422f7b5dbe59c94d935c8cf92f
|
|
| BLAKE2b-256 |
3351d724a492bd5fbe64b24296dbd8ca72ed649f7eef358f7348f93a148ecc8b
|