[Python-Dev] Avoiding file descriptors leakage in subprocess.Popen()

Mark Seaborn mrs at mythic-beasts.com
Sun Jun 14 17:42:55 CEST 2009


Facundo Batista <facundobatista at gmail.com> wrote:

> errpipe_read, errpipe_write = os.pipe()
> try:
>     try:
>         .....
>         .....
>         .....
>         .....
>         .....
>         .....
>     finally:
>         os.close(errpipe_write)
>     .....
>     .....
>     .....
> finally:
>     os.close(errpipe_read)
> 
> I just don't like a huge try/finally... but as FDs are just ints, do
> you think is there a better way to handle it?

I use a convenience function like this, so that GC takes care of the FDs:

def make_pipe():
    read_fd, write_fd = os.pipe()
    return os.fdopen(read_fd, "r"), os.fdopen(write_fd, "w")

Mark


More information about the Python-Dev mailing list