fork after creating temporary file using NamedTemporaryFile

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rparimi@gmail.com

    fork after creating temporary file using NamedTemporaryFile

    Hello pythoners,

    When I create temporary file using the tempfile module, and forkI)
    later on in my program, I always see errors when the program exits. Is
    this because the child process deletes temp file?
    Here's a stripped down version of my script that exhibits this
    problem:

    #!/usr/bin/python

    import os
    import tempfile
    import sys

    cmd = []
    cmd.append('/bin/ls')
    cmd.append('-l')
    cmd.append('/tmp')

    foo = tempfile.NamedT emporaryFile(mo de='w+b')

    pid = os.fork()
    if pid:
    print 'I am parent'
    else:
    print 'I am child'
    sys.exit(0)

    $ python sub.py
    I am child
    I am parent
    Exception exceptions.OSEr ror: (2, 'No such file or directory', '/tmp/
    tmp-mZTPq') in <bound method _TemporaryFileW rapper.__del__ of <closed
    file '<fdopen>', mode 'w+b' at 0xb7d2a578>igno red


    How can these warnings be avoided? I tried to catch this exception
    using try/except but it didn't work.

    thanks!
  • Lawrence D'Oliveiro

    #2
    Re: fork after creating temporary file using NamedTemporaryF ile

    In message <g5ikc7$k52$01$ 1@news.t-online.com>, Sebastian "lunar" Wiesner
    wrote:
    Relying on the destructor is *always* a bad idea, you should always
    close files explicitly!
    There shouldn't be any problem with files opened read-only.

    Comment

    Working...