Skip to main content

Tool to automatically reap sub-processes.

Project description

This module provides a subprocess.Popen()-compatible reap.Popen() function that has the nice property that if the parent process dies the child process will also be reaped.

Normally, orphaned child processes will end up re-parented to the init(1) process and then live on until they finally complete of their own volition. This often (usually?) isn’t the desired behavior and it can be really handy to ensure that when a parent process dies all the child processes will be automatically cleaned up.

The reap module accomplishes this by inserting a small intermediate process between the parent and the child. This intermediate process periodically checks to see if its parent PID has changed (indicating that the parent died and we have been reparented to init(1) or some other init(1)-like process), and if a change is detected then it kills the child before itself exiting. This ensures that any child processes you create will not later live on as orphaned processes.

Using pyreap is as easy as using subprocess.Popen(), and in fact all arguments are simply passed to subprocess.Popen(); the reap.Popen() function also returns a subprocess.Popen object that you can use just like you normally would.

The following demonstrates how subprocess.Popen() behaves with an orphaned process (note the orphaned ‘sleep 1000’ lives on even after the Python interpreter has closed):

~$ python3
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.Popen(['/usr/bin/env', 'sleep', '1000'])
<subprocess.Popen object at 0x10fc67d30>
>>>
~$ ps aux | grep sleep
greent7          85310   0.0  0.0  4268280    656 s026  S+    4:57pm   0:00.01 grep sleep
greent7          85308   0.0  0.0  4268156    504 s026  S     4:57pm   0:00.01 sleep 1000

The following demonstrates how reap.Popen() solves the orphan issue:

greent7@avocado:~$ python3
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import reap
>>> reap.Popen(['/usr/bin/env', 'sleep', '1000'])
<subprocess.Popen object at 0x10bac9d30>
>>>
greent7@avocado:~$ ps aux | grep sleep
greent7          85319   0.0  0.0  4287736    704 s026  S+    4:57pm   0:00.01 grep sleep

Note how the ‘sleep 1000’ process is reaped after the Python interpreter shuts down.

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

pyreap-0.9.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

pyreap-0.9.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file pyreap-0.9.2.tar.gz.

File metadata

  • Download URL: pyreap-0.9.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for pyreap-0.9.2.tar.gz
Algorithm Hash digest
SHA256 9fa5099608e6990480f0f9fd6af6a2c8a8a790fb96f84b0c81121f8471575322
MD5 97a3c429aa3d9fc0a37ca3da08e7a8e3
BLAKE2b-256 9a9c94966683e0f9f2221a49e5f884199ee44fc0e64daad7a72ec270632e249c

See more details on using hashes here.

File details

Details for the file pyreap-0.9.2-py3-none-any.whl.

File metadata

  • Download URL: pyreap-0.9.2-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for pyreap-0.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5ef856aaa9b20ffd3c523515b07063c8cd2937843d5b087117bb6db5a453cd0e
MD5 d78779eef7ade741a554b1c82ef3de91
BLAKE2b-256 70df26da0664baca8ca9f1c72f1b6e2bc3acf6f799629eeec8801cf1d7d7e802

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