How can I know when a sub-process is effectively initialized?
exarkun at divmod.com
Thu Feb 26 15:59:38 EST 2009
On Thu, 26 Feb 2009 12:27:26 -0800 (PST), Giampaolo Rodola' <gnewsg at gmail.com> wrote:
>I'm working on a Python module called psutil  for reading process
>information in a cross-platform way.
>I'm having a problem with psutil test suite.
>Almost all the test cases we implemented so far look like this:
> test_process = subprocess.Popen(sys.executable, stdout=DEVNULL,
> time.sleep(0.1) # XXX: provisional, give some time the sub
>process to initialize
> p = psutil.Process(test_process.pid)
> # start test here
>As you can see we put a time.sleep(0.1) call after subprocess.Popen()
>as a provisional workaround to let the sub process initialize
>We're searching for some kind of way to know when the child process is
>properly initialized so that we can actually start testing cases
>Does someone has an idea how could we do that?
"Properly initialized" is application specific. Generally, what you want
to do is have the child process tell you when it's ready. A common way
to do this is for the child to write some bytes to a file descriptor the
parent is monitoring when it is ready. The parent then just waits for
those bytes. It looks like you entirely control the child in this case,
so that should be possible here.
Alternatively, maybe by "properly initialized" you mean something very
specific about how subprocess.Popen creates processes. If so, can you
elaborate a bit?
More information about the Python-list