New submission from Christian Heimes:

I propose the addition of an 'afterfork' module. The module shall fulfill a 
similar task as the 'atexit' module except that it handles process forks 
instead of process shutdown.

The 'afterfork' module shall allow libraries to register callbacks that are 
executed on fork() inside the child process and as soon as possible. Python 
already has a function that must be called by C code: PyOS_AfterFork(). The 
'afterfork' callbacks are called as the last step in PyOS_AfterFork().

Use case example:
The tempfile module has a specialized RNG that re-initialized the RNG after 
fork() by comparing os.getpid() to an instance variable every time the RNG is 
accessed. The check can be replaced with an afterfork callback.

Open questions:
How should the afterfork() module handle exceptions that are raised by 

I'm going to use as much code from atexitmodule.c as possible. I'm going to 
copy common code to a template file and include the template from 
atexitmodule.c and afterforkmodule.c with some preprocessor tricks.

assignee: christian.heimes
components: Extension Modules, Interpreter Core
keywords: needs review
messages: 175878
nosy: christian.heimes
priority: normal
severity: normal
status: open
title: Add an 'afterfork' module
type: enhancement
versions: Python 3.4

Python tracker <>
Python-bugs-list mailing list

Reply via email to