|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
||||
|
||||
|
Closing pipes
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? |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
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: self.pipe.close() 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? |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
||||
|
||||
|
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
![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Closing pipes |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|