You have a lot of problems with your code. The first is:
def command_is_running(pid): with open('/proc/%d/stat' % pid) as stat: stats = stat.read() return ' R ' in stats def wait_command_processes_line(pid):
- employment cycle. It will consume as much CPU as it can, showing .../stat until R disappears. It is not recommended to work with an additional processor when you are trying to get the exact CPU time.
I donโt know how to put the process to sleep until the state of another process changes, so I canโt offer an effective replacement for the busy cycle. But this does not matter because of the second problem: the state of the process is not as predictable as you want.
You made the assumption that the process will become executable at the moment when you write some data in its pipe, and will remain on while working throughout the process of processing this input. It would be very difficult to guarantee that this is true. You said that the "IO drive is rare", but you will need to do better than this and completely eliminate it, including page errors. This is complicated, and you probably didnโt. Therefore, I think that your problem is not that /proc/PID/stat contains incorrect information, but that you are reading it at the wrong time.
You can get around the disk I / O problem by treating state D in the same way as R But he still looks stupid.
Instead of looking at the health of the process, you should find a better indicator that the child process has finished processing the very last line of input. You said that "maybe it prints something in stdout". If you can arrange for him to always print something in stdout for each line of input, then the parent process can wait for this output and an example of using the child processor when it appears.
If you cannot force the child process to provide an external completion indication for each input line, an alternative would be to look at it with the input line when it tries to read the next line of input. Basically, you will use ptrace to implement a specialized strace utility that writes read time on the input channel, writing a string to the pipe only after your trace tells you what it is trying to read.
Perhaps you can even do this with strace and some smart shell scripts.
Another option for this idea would be to use gdb to set a breakpoint in the child process at the beginning of its input processing cycle, and set up a script that will execute every time a breakpoint is hit. The script will collect time information, write the next line to the pipe, and then execute gdb cont .