How to close all files between fork and exec?

Aahz Maruch aahz at panix.com
Fri Jan 19 10:45:00 EST 2001


In article <yv2lms8lyre.fsf at lionsp093.lion-ag.de>,
Harald Kirsch  <kirschh at lionbioscience.com> wrote:
>aahz at panix.com (Aahz Maruch) writes:
>>
>> from fork import open
> 
>Hmm, this is becoming somehow interesting because I have got no idea
>why you think this relates in any way to my question. Either you try
>to tease me or we have a communication problem. But I think I should
>stay calm and try to start all over:
>
>Suppose you have the assignment to write a module for me which, for
>whatever reasons, has to fork/exec certain subprocesses. I want to use
>your module as a service in my software. But my software will have a
>whole bunch of files open when it calls your module. How did your
>module keep track of all these files so that it is able to close them
>all before execing. --- If you now say that your module cannot do that
>and that it would be the obligation of my software to close unneded
>files before calling your module, I at least start to understand your
>arguments. However I maintain the opinion that this would be nonsense.

To expand on my terse comment, "from fork import open" replaces the
builtin function open() with the function from the fork module.  This
function is a thin wrapper around the builtin open() that tracks all
files that have ever been opened.  You can't write a wrapper for close()
unless you also design a UserFile class that you return from open; it's
not that useful, anyway, because a standard Python idiom is to let files
fall out of scope and let them close themselves (which unfortunately
won't happen in programs that use the fork module because the fork module
will retain a reference; it's a trade-off).

When you call the actual fork/exec function, it just checks to see which
of the stored file handles are still open and then closes them.
-- 
                      --- Aahz (Copyright 2001 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

I guess I mostly see Life [tm] as a process of closing doors that you
might want to go through.  --Aahz



More information about the Python-list mailing list