Discussion:
[Tutor] Getting rid of temp files after fork/exec?
Evgeny Roubinchtein
2000-01-20 06:38:01 UTC
Permalink
This is Unix-specific, but since I understand many people on this list
use Linux/Unix I thought I'd ask.

I have a function that writes some data to a temp file, then fork()s; the
child execv()s a program passing to it the name of the temp file as an
argument, and the parent does some cleanup in the GUI and then returns.
The problem is that leaves the temp file around, since the execv() doesn't
really return, and I don't like that. I thought this was a common enough
thing that someone might know what people normally do to get rid of the
temp file.

For reference, here's the function's code (it's part of a simple GUI to
select a bunch of files to play with RealAudio Player, in case you are
wondering).

def call_rvplayer(self):
l = []
for f in range(self.playlist.size()):
l.append('file://' + self.ra_dir + self.playlist.get(f) +
'.ra')
tn = tempfile.mktemp()
rf = tn + '.ram'
t = open(rf, 'w')
t.write(string.join(l, '\n'))
t.write('\n')
t.close()
pid = os.fork()
# XXX: how do I get rid of the temp file?
if pid == 0: # child
tempfile.template = None # per Library Reference
os.execv('/home/eroubinc/RealPlayerG2/realplay',
('/home/eroubinc/RealPlayerG2/realplay', rf))

else: # parent
self.playlist.delete(0, self.playlist.size())
return
--
Evgeny

Old mail has arrived.
Evgeny Roubinchtein
2000-01-20 23:56:20 UTC
Permalink
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 - ***@python.org
ER> http://www.python.org/mailman/listinfo/tutor
ER>
ER>
--
Evgeny

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