Skip to content
This repository was archived by the owner on Apr 24, 2018. It is now read-only.
This repository was archived by the owner on Apr 24, 2018. It is now read-only.

Nice signal handling? #6

Open
Open
@hjwp

Description

@hjwp

Here's how you set up a signal handler for the current process, that makes sure any child processes that terminate then properly die (rather than sitting around in "defunct" status leaking resources):

def wait_for_children(signal, stack_frame):
    p = psutil.Process(os.getpid())
    children = p.get_children()
    for child in children:
        try:
            logging.debug('Waiting for child %s' % (child,))
            os.wait4(child.pid, os.WNOHANG)
        except OSError:
            logging.debug('Exception while waiting for child with pid %s' % (child.pid,))

signal.signal(signal.SIGCHLD, wait_for_children)

pretty arcane stuff! there must be a nicer solution...

You can also use signal to special-case ctrl+c, or any other OS signals. This may be too low-level for most users tho, so maybe not such a high priority.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions