Skip to main content

Let orphan processes suicide

Project description

Let child processes in Python suicide if they became orphans.

Installation

pip install orphanage

Don’t forget to put it in setup.py / requirements.txt.

Usage

from orphanage import exit_when_orphaned

exit_when_orphaned()

Motivation

Some application server softwares (e.g. Gunicorn) work on a multiple-process architect which we call the master-worker model. They must clean up the worker processes if the master process is stopped, to prevent them from becoming orphan processes.

In the gevent-integration scene, the worker processes of Gunicorn poll their ppid in an user thread (a.k.a greenlet) to be orphan-aware. But the user thread may be hanged once the master process crashed because of the blocked writing on a pipe, the communicating channel between master process and worker processes.

We want to perform this ppid polling in a real kernel thread. That is the intent of this library.

Principle

This library spawns an internal thread to poll the ppid at regular intervals (for now it is one second). Once the ppid changed, the original parent process should be dead and the current process should be orphaned. The internal thread will send SIGTERM to the current process.

In the plan, the prctl & SIGHUP pattern may be introduced in Linux platforms to avoid from creating threads. For now, the only supported strategy is the ppid polling, for being portable.

Alternatives

CaoE is an alternative to this library which developed by the Douban Inc. It uses prctl and a twice-forking pattern. It has a pure Python implementation without any C extension compiling requirement. If you don’t mind to twist the process tree, that will be a good choice too.

Contributing

If you want to report bugs or request features, please feel free to open issues on GitHub.

Of course, pull requests are always welcome.

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

orphanage-0.1.0.tar.gz (6.5 kB view hashes)

Uploaded Source

Built Distributions

orphanage-0.1.0-pp3510-pypy3_510-macosx_10_13_x86_64.whl (8.7 kB view hashes)

Uploaded PyPy macOS 10.13+ x86-64

orphanage-0.1.0-pp2510-pypy_41-macosx_10_13_x86_64.whl (12.3 kB view hashes)

Uploaded PyPy macOS 10.13+ x86-64

orphanage-0.1.0-cp36-cp36m-manylinux1_x86_64.whl (16.7 kB view hashes)

Uploaded CPython 3.6m

orphanage-0.1.0-cp36-cp36m-macosx_10_13_x86_64.whl (10.1 kB view hashes)

Uploaded CPython 3.6m macOS 10.13+ x86-64

orphanage-0.1.0-cp27-cp27mu-manylinux1_x86_64.whl (19.7 kB view hashes)

Uploaded CPython 2.7mu

orphanage-0.1.0-cp27-cp27m-macosx_10_13_x86_64.whl (10.1 kB view hashes)

Uploaded CPython 2.7m macOS 10.13+ x86-64

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