[pygtk] how to close the X connection file descriptor?

James Henstridge james@daa.com.au
Thu, 16 Nov 2000 17:39:15 +0800 (WST)


On Wed, 15 Nov 2000, George Young wrote:

> Yes, fcntl worked great!  I'm stuck with python 1.5.2 for a while, so I can't use
> the sysconf from later releases but for now a guess is OK for the greatest file
> descriptor:
>         pid = os.fork()
>         if  pid: # in parent
>             time.sleep(0.1) # block briefly(100 ms.) to give it time to fail...
>             wpid,exitstatus = os.waitpid(pid, os.WNOHANG) 
>             os._exit(exitstatus)
> 
>         else: # in child
>             # Set close-on-exec for all (guess 30 max) file descriptors.
>             for fd in range(30):

And if you don't want to close stdin,out,err, change this to range(3,30)

>                 try:
>                     fcntl.fcntl(fd, FCNTL.F_SETFD, 1)

you should probably use FCNTL.FD_CLOEXEC here, rather than the constant 1.

>                 except IOError: # Probably fd is not open.
>                     pass
>             os.setsid()
>             try:
>                 os.execvp(cmd, args) # does not return except on error
>             except OSError:
>                 sys.exit(2)
> 

James.