Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Fork and detach the current processe.

Project Description

Fork and detach the current process.

[![Build Status](](


The detach package contains a context manager called Detach. It is used with with with statement to fork the current process and execute code in that process. The child process exits when the context manager exits. The following parameters may be passed to Detach to change its behavior:

  • stdout - Redirect child stdout to this stream.
  • stderr - Redirect child stderr to this stream.
  • stdin - Redirect his stream to child stdin.
  • close_fds - Close all file descriptors in the child excluding stdio.
  • exclude_fds - Do not close these file descriptors if close_fds is True.
  • daemonize - Exit the parent process when the context manager exits.


### Simple Fork with STDOUT

import detach, os, sys

with detach.Detach(sys.stdout) as d:
print(“forked child with pid {}”.format(
print(“hello from child process {}”.format(os.getpid()))

### Daemonize

import detach from your_app import main

def main():
“”“Your daemon code here.”“”
with detach.Detach(daemonize=True) as d:
print(“started process {} in background”.format(pid))

### Call External Command

import detach, sys pid =[‘bash’, ‘-c’, ‘echo “my pid is $$”’], stdout=sys.stdout) print(“running external command {}”.format(pid))


Copyright (c) 2014 Ryan Bourgeois. This project and all of its contents is licensed under the BSD-derived license as found in the included [LICENSE][1] file.

[1]: “LICENSE”

Release History

This version
History Node


History Node


Download Files

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

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(3.4 kB) Copy SHA256 Hash SHA256
Source None Jun 28, 2014

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Kabu Creative Kabu Creative UX & Design Google Google Cloud Servers Fastly Fastly CDN StatusPage StatusPage Statuspage DigiCert DigiCert EV Certificate