The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Closing pipes
Discuss Closing pipes in the Python Programming forum on Dev Shed. Closing pipes Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 6th, 2003, 06:16 AM
|
 |
Wacky hack
|
|
Join Date: Apr 2001
Location: London, England
Posts: 513
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
|
|
|
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?
|

March 6th, 2003, 08:40 AM
|
 |
Wacky hack
|
|
Join Date: Apr 2001
Location: London, England
Posts: 513
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
|
|
|
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
|
 |
Wacky hack
|
|
Join Date: Apr 2001
Location: London, England
Posts: 513
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
|
|
|
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?
|

March 26th, 2003, 11:04 AM
|
|
Registered User
|
|
Join Date: Mar 2003
Location: Atlanta
Posts: 9
Time spent in forums: 35 m
Reputation Power: 0
|
|
|
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
|
 |
Wacky hack
|
|
Join Date: Apr 2001
Location: London, England
Posts: 513
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
|
|
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 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|