1.1.1. radical.utils.atfork package

1.1.1.1. Submodules

1.1.1.2. radical.utils.atfork.atfork module

This module implements a pthread_atfork() work-a-like mechanism for all fork() calls made from the Python os module. Any time a fork() is called from Python a set of unique callbacks will be made in each of the following three states:

  • Preparing to fork - Immediately before the fork call is made.

  • In the child after fork - Immediately after the fork in the child process.

  • In the parent after fork - Immediately after the fork in the parent process.

NOTE: The parent callback will executed also if the fork failed.

To use this module, first import it early on your programs initialization:

import atfork
atfork.monkeypatch_os_fork_functions()

That will stub out os.fork and os.forkpty with wrapped versions implementing the enhanced behavior.

Next, register your atfork actions by calling atfork.atfork:

atfork.atfork(prepare=my_lock.acquire,
              parent=my_lock.release,
              child=my_lock.release)

No API to unregister an atfork call is provided. If you are concerned about resource usage by references your callable holds, consider using weakref’s within your callable.

radical.utils.atfork.atfork.atfork(prepare=None, parent=None, child=None)[source]

A Python work-a-like of pthread_atfork.

Any time a fork() is called from Python, all ‘prepare’ callables will be called in the order they were registered using this function.

After the fork (successful or not), all ‘parent’ callables will be called in the parent process. If the fork succeeded, all ‘child’ callables will be called in the child process.

No exceptions may be raised from any of the registered callables. If so they will be printed to sys.stderr after the fork call once it is safe to do so.

radical.utils.atfork.atfork.child_after_fork_release()[source]

Call all child after fork callables, release lock and print all all child callback exceptions.

radical.utils.atfork.atfork.monkeypatch_os_fork_functions()[source]

Replace os.fork* with wrappers that use ForkSafeLock to acquire all locks before forking and release them afterwards.

radical.utils.atfork.atfork.os_fork_wrapper()[source]

Wraps os.fork() to run atfork handlers.

radical.utils.atfork.atfork.os_forkpty_wrapper()[source]

Wraps os.forkpty() to run atfork handlers.

radical.utils.atfork.atfork.parent_after_fork_release()[source]

Call all parent after fork callables, release the lock and print all prepare and parent callback exceptions.

radical.utils.atfork.atfork.prepare_to_fork_acquire()[source]

Acquire our lock and call all prepare callables.

1.1.1.3. radical.utils.atfork.stdlib_fixer module

This module provides code to setup appropriate atfork() calls within the Python standard library in order to make it safe for use in programs that mix fork() and threads.

Import this and call the appropriate fixers very early in your program’s initialization sequence, preferrably -before- importing the modules you need to fix.

Provided fixers:

fix_logging_module()

TODO(gps): Audit more of the stdlib and provide necessary fixers. In 2.4.5 the following additional stdlib modules use locks:

threading.Condition and similar classes could use it. Queue cookielib mimetools _strptime

exception radical.utils.atfork.stdlib_fixer.Error[source]

Bases: Exception

radical.utils.atfork.stdlib_fixer.fix_logging_module()[source]

1.1.1.4. Module contents