March 6th, 2003, 06:16 AM
I've got a bit of code that opens a new pipe with popen, to start mencoder. When I call the close() function on it, it takes an unacceptably long time to close the pipe and return to the main thread.
Are there any better ways of handling external commands?
March 6th, 2003, 08:40 AM
Oh, I found out myself, so if anyone wants to know:
os.popen will wait for the process to finish before it actually closes the pipe and returns the result to you, which is why it takes so long. popen2.popen2 won't however, so I've just used the popen2 module and it now works fine.
March 14th, 2003, 09:34 AM
I thought I had this problem cracked, but it seems not! I now use the following code:
To open the pipe:
(self.pipe, self.junk) = popen2.popen2(command)
To close the pipe:
Now when I close it, Python releases the pipe immediately, but it seems to just disconnect itself from the process and leave it running,which is of no use whatsoever! using sys.popen() and the associated close() methods worked OK, but very very slowly when closing. popen2.popen2 works more quickly, but it doesn't close the process.
How can I actually close the process, and do it quickly? Or will I just have to put up with zero responsiveness for a while waiting for the pipe to close properly?
March 26th, 2003, 11:04 AM
Interesting problem. I'd suggest a two pronged approach. First, if the sub-process is dragging on too long, why not just kill it? ( Popen3 and Popen4 have a pid method. )
Second of all, maybe it would be quicker (after having killed the sub-process) to just loop through and discard the unwanted data than it would be to close a "full" pipe.
March 26th, 2003, 11:22 AM
Yes, I ended up implementing it with Popen3, taking the pid and killing it, then clearing the zombie with waitpid(). I was thinking of making quite a complicated but more robust system with forks and exec() but I never got round to it, as Popen3 seems to work fine