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.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyreap-0.9.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5

File hashes

Hashes for pyreap-0.9.1.tar.gz
Algorithm Hash digest
SHA256 88fc6117a75879d54611cc3ef21e1ac5f2188209bf739ba6c523d35e5026d157
MD5 7fd1f7acce1cd5fbb1630691accb9480
BLAKE2b-256 85bfa4c3efddca5838bb8eb175bd442f8c7d5c8deecb844d99274bf638c404ad

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyreap-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5

File hashes

Hashes for pyreap-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2d6884cb3313c587488c2170cda5a8e7da90f4583cd9c13cc5086b37ccafe423
MD5 b7826078cd7d0f616154d8f7b685ae09
BLAKE2b-256 0ed0db7349f4785aaab01e118169715db7073a443ded8081dfeea420d8af22ae

See more details on using hashes here.

Supported by

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