Solved! Re: [Tutor] Getting rid of temp files after fork/exec?

Evgeny Roubinchtein eroubinc@u.washington.edu
Thu, 20 Jan 2000 15:56:20 -0800 (PST)


I think I got it now.  My solution was to call fork() twice, like so:

# cut --- rest of function unchanged. --- cut 
        pid = os.fork()
        if pid == 0: # first child
            tempfile.template = None # per Library Reference
            pid2 = os.fork()
            if pid2 == 0:  # grandhild -- exec the program
                os.execv('/home/eroubinc/RealPlayerG2/realplay',
                     ('/home/eroubinc/RealPlayerG2/realplay', rf))
            else: # child - wait on grandchild, then remove the file
                (p, s) = os.waitpid(pid2, 0)
                os.unlink(rf)
                sys.exit(0)
        else: # parent
            self.playlist.delete(0, self.playlist.size())
            return

The child does become a zombie in this scheme, but not for long; does this
have any similarity to what is normally done?

On Wed, 19 Jan 2000, Evgeny Roubinchtein wrote:

ER> This is Unix-specific, but since I understand many people on this list
ER> use Linux/Unix I thought I'd ask.  
ER> 
ER> I have a function that writes some data to a temp file, then fork()s; the
ER> child execv()s a program passing to it the name of the temp file as an
ER> argument, and the parent does some cleanup in the GUI and then returns.
ER> The problem is that leaves the temp file around, since the execv() doesn't
ER> really return, and I don't like that. I thought this was a common enough
ER> thing that someone might know what people normally do to get rid of the
ER> temp file.
ER> 
ER> For reference, here's the function's code (it's part of a simple GUI to
ER> select a bunch of files to play with RealAudio Player, in case you are
ER> wondering).
ER> 
ER>     def call_rvplayer(self):
ER>         l = []
ER>         for f in range(self.playlist.size()):
ER>             l.append('file://' + self.ra_dir + self.playlist.get(f) +
ER> '.ra')
ER>         tn = tempfile.mktemp()
ER>         rf = tn + '.ram'
ER>         t = open(rf, 'w')
ER>         t.write(string.join(l, '\n'))
ER>         t.write('\n') 
ER>         t.close()
ER>         pid = os.fork()
ER>         # XXX: how do I get rid of the temp file?
ER>         if pid == 0: # child
ER>             tempfile.template = None # per Library Reference
ER>             os.execv('/home/eroubinc/RealPlayerG2/realplay',
ER>                      ('/home/eroubinc/RealPlayerG2/realplay', rf))
ER> 
ER>         else: # parent
ER>             self.playlist.delete(0, self.playlist.size())
ER>             return
ER> 
ER> --
ER> Evgeny 
ER> 
ER> Old mail has arrived.
ER> 
ER> 
ER> _______________________________________________
ER> Tutor maillist  -  Tutor@python.org
ER> http://www.python.org/mailman/listinfo/tutor
ER> 
ER> 

--
Evgeny 

A)bort, R)etry, I)gnore, V)alium?