memcache-dynamo
. Maybe I shouldn’t have, but it happened, and I’ve had to fix my bugs.The problem is, the parent would never notice when the child exited. Other signals were being handled fine, but the
SIGCHLD
handler was never called.This is because, although it says “add” signal handler, the API is really more of a “set” signal handler, replacing any that are already there. Also, the Unix event loop needs to know about exiting children in order to clean up the subprocess resources, so it sets its own handler.
As it turns out, the correct way to go about this is to use a “child watcher” to allow outside code to react to
SIGCHLD
. One should call get_child_watcher
and then, on the returned object, add_child_handler
. This takes a PID argument, so it can only be done once the child has been created. At minimum:proc = await asyncio.create_subprocess_exec(…)
watcher = asyncio.get_child_watcher()
watcher.add_child_handler(proc.pid, onChildExit)
This “onChildExit” is the name of a callback function, which be called with the PID and returncode as arguments. If more positional arguments were given to
add_child_handler
, then those will also be passed to the callback when it is called.The other signals can be handled in the usual manner, but
SIGCHLD
is special this way.(This applies to Unix/macOS only, as Windows doesn’t have POSIX signals. Maybe the shiny new subsystem does, but in general, it doesn’t.)
No comments:
Post a Comment