os.system(), HTTPServer, and finishing HTTP requests

Erik Johnson ej
Mon Aug 16 20:11:18 EDT 2004


Ah.... Thank you for your reply, Donn. I am not much of a systems programmer
(obviously) and was thinking that system() calls were implemented
fundamentally
different than fork().

For anyone else who might be reading...
Using str(os.fstat()) to print the file descriptors, I was a little
surprised to find 3, 4, 5, 6 all in
 use and having identical parameters (0, 1, & 2 being STDIN, STDOUT,
STDERR).
I'm not sure why those would all be open, but this little bit seems to
resolve the problem
(it throws OSError when it hits the first invalid file descriptor).

for fd in xrange(3, 256):
    try:
        os.close(fd)
    except OSError:
        break

If you know a better/smarter way to effect the same thing, I'd be glad to
hear about it.

Thanks again! :)
-ej


"Donn Cave" <donn at drizzle.com> wrote in message
news:1092631376.357585 at yasure...
> Quoth "Erik Johnson" <ej <at> wellkeeper <dot> com>:
> ...
> |      But if I make this same os.system() call from within my own
HTTPServer,
> | then the browser that is making the request is left hanging until that
> | sleeping grandchild is done. It's like there is still a socket
connection
> | between it and my browser?!? But the grandchild is not forked from the
> | server - it's an os.system() call!  It should not be inherting any file
> | descriptors and such, right?
>
> On the contrary, system() does call fork - basically,
>
>    def system(command):
>        pid = fork()
>        if pid:
>            return waitpid(pid, 0)[1]
>        else:
>            execve('/bin/sh', ['sh', '-c', command], environ)
>
> and indeed file descriptors will be inherited.  You'll
> have to explicitly close the socket.
>
> Donn Cave, donn at drizzle.com





More information about the Python-list mailing list