Skip to main content

Detach

Fork and detach the current process.

[![Build Status](https://travis-ci.org/BlueDragonX/detach.svg)](https://travis-ci.org/BlueDragonX/detach)

Usage

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.

Examples

### Simple Fork with STDOUT

import detach, os, sys

with detach.Detach(sys.stdout) as d:
if d.pid:

print(“forked child with pid {}”.format(d.pid))

else:

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:
if d.pid:

print(“started process {} in background”.format(pid))

else:

main()

### Call External Command

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

License

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]: https://github.com/bluedragonx/detach/blob/master/LICENSE “LICENSE”

Metadata

Release files for detach 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for detach 1.0
File Size Uploaded
detach-1.0.tar.gz 3.4 kB Details

Release files / detach-1.0.tar.gz

Download URL detach-1.0.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
95b7164735506b9f12181c717d74289cea7f300cb5e1ec1970a3f9f45860683f
BLAKE2b-256 checksum
How to use checksums
834c6028198445fe35f6ba7a12bca5ec19d8273d2b259b50e8ea0564348a13c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0 This release

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page