Why doesn't everybody want this?
Nov. 20th, 2003 12:44 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
I'd like a function, in any language I'm using, to run a child process and collect its output, but to also kill that process and all of its children after some timeout. This is okay to do in C, provided of course that you've immersed yourself in Advanced Programming in the Unix Environment, and in fact we have a program that runs a child and kills it after some timeout. Needed to hack it to set up its own process group to really get all of the children, but it works.
So now I want to do the same thing, but in Python. Except that the environment I'm running under uses threads. Uh-oh. And I can't set a signal handler not-in-the-main-thread, so a direct port fails. Oh, and running our C binary fails, too, mysteriously. (Could be that the top-level process doesn't intelligently handle SIGTERM.) I can actually set a timer as a thread pretty easily in Python, but calling os.kill() from the timeout handler doesn't seem to have an effect, or more appropriately, it kills the top-level process but not any of the children. Grr.
So now I want to do the same thing, but in Python. Except that the environment I'm running under uses threads. Uh-oh. And I can't set a signal handler not-in-the-main-thread, so a direct port fails. Oh, and running our C binary fails, too, mysteriously. (Could be that the top-level process doesn't intelligently handle SIGTERM.) I can actually set a timer as a thread pretty easily in Python, but calling os.kill() from the timeout handler doesn't seem to have an effect, or more appropriately, it kills the top-level process but not any of the children. Grr.
no subject
Date: 2003-11-20 12:25 pm (UTC)signals and threads also mix badly. Something to note is that SIGIO is SIG_IGN in python, and this is inherited; doesn't sound like your problem, but it's something to know when doing that.
I asked about a similar problem ("why isn't there a way to exec a pipeline with array args") and got the answer that once you get beyond commands.getstatusoutput, you tend to have hard-to-generalize things that you want. I don't agree, but haven't gotten around to posting my pipeline class.
If the child isn't dealing with timeouts itself, consider just setting an alarm() after the fork, instead of having the parent try to do it...