How to correctly fork a child process in twisted that does not use anything from twisted (but uses data from the parent process) (eg to process a “snapshot” of some data from the parent process and write it to file, without blocking)?
It seems if I do anything like clean shutdown in the child process after os.fork()
, it closes some of the sockets / descriptors in the parent process; the only way to avoid that that I see is to do os.kill(os.getpid(), signal.SIGKILL)
, which does seem like a bad idea (though not directly problematic).
(additionally, if a dict is changed in the parent process, can it be that it will change in the child process too? Quick test shows that it doesn't change, though. OS/kernels are debian stable / sid)
IReactorProcess.spawnProcess
(usually available as from twisted.internet import reactor; reactor.spawnProcess
) can spawn a process running any available executable on your system. The subprocess does not need to use Twisted, or, indeed, even be in Python.
Do not call os.fork
yourself. As you've discovered, it has lots of very peculiar interactions with process state, that spawnProcess
will manage for you.
Among the problems with os.fork
are:
fork()
correctly and will simply crash your program if you attempt to use them after fork
but before exec
. exec
- but no such flag to have them close on fork
. So any files (including log files, and again, any background temp files opened by libraries you might not even be aware of) can get silently corrupted or truncated if you don't manage access to them carefully.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.