[Python-Dev] Functionality in subprocess.Popen.terminate()
janzert at janzert.com
Tue Aug 4 17:24:23 CEST 2009
Eric Pruitt wrote:
> On Tue, Aug 4, 2009 at 04:27, Nick Coghlan<ncoghlan at gmail.com> wrote:
>> Eric Pruitt wrote:
>>> In my GSoC project, I have implemented asnychronous I/O in
>>> subprocess.Popen. Since the read/write operations are asynchronous, the
>>> program may have already exited by the time one calls the asyncread
>>> function I have implemented. While it returns the data just fine, I have
>>> come across an issue with the TerminateProcess function in Windows: if
>>> the program has already exited, when subprocess.Popen.Terminate calls
>>> the Windows built-in "TerminateProcess" function, an "access denied"
>>> error will occur. Should I just make it so that this exception is simply
>>> ignored or perform some kind of check to see if the process exists
>>> beforehand? If the latter, I have been unable to find a way to do so, to
>>> my liking at least. The solutions I saw would require code that seems a
>>> bit excessive to me.
>> I'm pretty sure we already ignore some spurious error messages in cases
>> like calling flush() in file.close(). I would suggest checking what the
>> io module does in such cases and see what kind of precedent it sets.
>> Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
> Sounds good enough to me but I was wondering if it might be a good
> idea to add a function like "pidinuse" to subprocess as a whole that
> would determine if a process ID was being used and return a simple
> boolean value. I came across a number of people searching for a way to
> determine if a PID was running (Google "python check if pid exists")
> so it seems like the implemented functionality would be of use to the
> community as a whole, not just my wrapper class.
I'm not sure of the actual details but it seems from your description
that even if you check first a race condition will still exist.
Specifically the subprocess could terminate after the check and before
the TerminateProcess call. So it seems better just to call
TerminateProcess and then correctly handle any possible error.
More information about the Python-Dev