Thread: Closing pipes

    #1
  1. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14

    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. #2
  3. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    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.
  4. #3
  5. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    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?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    9
    Rep 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.
  8. #5
  9. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    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

IMN logo majestic logo threadwatch logo seochat tools logo