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


Release history Release notifications

This version

0.1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for orphanage, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size orphanage-0.1.0-cp27-cp27m-macosx_10_13_x86_64.whl (10.1 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size orphanage-0.1.0-cp27-cp27mu-manylinux1_x86_64.whl (19.7 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size orphanage-0.1.0-cp36-cp36m-macosx_10_13_x86_64.whl (10.1 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size orphanage-0.1.0-cp36-cp36m-manylinux1_x86_64.whl (16.7 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size orphanage-0.1.0-pp2510-pypy_41-macosx_10_13_x86_64.whl (12.3 kB) File type Wheel Python version pp2510 Upload date Hashes View hashes
Filename, size orphanage-0.1.0-pp260-pypy_41-manylinux1_x86_64.whl (23.6 kB) File type Wheel Python version pp260 Upload date Hashes View hashes
Filename, size orphanage-0.1.0-pp3510-pypy3_510-macosx_10_13_x86_64.whl (8.7 kB) File type Wheel Python version pp3510 Upload date Hashes View hashes
Filename, size orphanage-0.1.0-pp360-pypy3_60-manylinux1_x86_64.whl (10.4 kB) File type Wheel Python version pp360 Upload date Hashes View hashes
Filename, size orphanage-0.1.0.tar.gz (6.5 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page