Hi Terry,

> Would this be a standard subprocess.call() invocation or is there a
> better way?

The former seems fine AFAICS.

> Would I need to create two global instances of /dev/null if I use it
> twice in the same function?

No, the file descriptor of the open Python File for /dev/null that you
have it dup(2)'d into the desired position, e.g. 2 if you're specifying
it's for stderr.

> How would the instances of /dev/null show up?

Clearly.

    $ sleep 42 </dev/null &>/dev/null 42>/dev/null &
    [1] 30839
    $ ls -l /proc/30839/fd
    total 0
    lr-x------ 1 ralph ralph 64 Apr  3 19:16 0 -> /dev/null
    l-wx------ 1 ralph ralph 64 Apr  3 19:16 1 -> /dev/null
    l-wx------ 1 ralph ralph 64 Apr  3 19:16 2 -> /dev/null
    l-wx------ 1 ralph ralph 64 Apr  3 19:16 42 -> /dev/null
    $

> I have the same references to /dev/pts/2 plus:
> 
> 1.  All of the GPIO pins appear to be there.
> 2.  /dev/urandom.
> 3.  /dev/gpiomem.
> 4.  anon_inode:[eventpoll11]
> 
> That's it.  Does that mean that my instances of /dev/null are killed
> when the function exits?

Yes, I think so.  It looks like they're being garbage collected by
Python's reference counter because the stdlib function you're passing
the File to doesn't keep a reference.

    $ python2
    >>> import os
    >>> def f(): null = open('/dev/null')
    ... 
    >>> os.system('ls -l /proc/%d/fd' % os.getpid())
    total 0
    lrwx------ 1 ralph ralph 64 Apr  3 19:13 0 -> /dev/pts/2
    lrwx------ 1 ralph ralph 64 Apr  3 19:13 1 -> /dev/pts/2
    lrwx------ 1 ralph ralph 64 Apr  3 19:13 2 -> /dev/pts/2
    0
    >>> f(), f(), f()
    (None, None, None)
    >>> os.system('ls -l /proc/%d/fd' % os.getpid())
    total 0
    lrwx------ 1 ralph ralph 64 Apr  3 19:13 0 -> /dev/pts/2
    lrwx------ 1 ralph ralph 64 Apr  3 19:13 1 -> /dev/pts/2
    lrwx------ 1 ralph ralph 64 Apr  3 19:13 2 -> /dev/pts/2
    0
    >>> 

Those three calls to open() through f() and yet all of them have been
closed.  There's nothing still referring to them after f() has returned.

    >>> def g(): null = open('/dev/null'); os.system('ls -l /proc/%d/fd' % 
os.getpid())
    ... 
    >>> g()
    total 0
    lrwx------ 1 ralph ralph 64 Apr  3 19:13 0 -> /dev/pts/2
    lrwx------ 1 ralph ralph 64 Apr  3 19:13 1 -> /dev/pts/2
    lrwx------ 1 ralph ralph 64 Apr  3 19:13 2 -> /dev/pts/2
    lr-x------ 1 ralph ralph 64 Apr  3 19:13 3 -> /dev/null
    >>> 

While a reference still exists, the local variable null in g(), the file
remains open.

So I don't expect you have to do anything to close it.  It's your choice
if you opening it once per program or once per function.  (I'd still go
for the former, but you've delivered the code.  :-)

Cheers, Ralph.

-- 
Next meeting:  Bournemouth, Tuesday, 2017-04-04 20:00
Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
New thread:  mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING
Reporting bugs well:  http://goo.gl/4Xue     / TO THE LIST OR THE AUTHOR

Reply via email to