[Tutor] Delete file before function ends

Steve Willoughby steve at alchemy.com
Mon Oct 6 20:20:29 CEST 2008


On Mon, Oct 06, 2008 at 02:15:06PM -0400, Adrian Greyling wrote:
> As a newbie, Alan, I was kinda scared you'd say that "threads" were the
> answer here!  (It sounds like someone is going to get sucked into a worm
> hole or something...)  Looks like the next class in my Python education is
> going to be "Threads 101"...

Threads are one approach, but if you aren't ready to jump into them,
that's not the only way to solve your problem.  However, please understand
that your application is trying to do something that's a bit more advanced
than the "newbie" level.  Any time you need to synchronize the operation
of multiple programs, there are complications and platform dependencies
involved that may not be as simple as you first think.

One natural, but naive, approach is to simply drop in a delay loop 
or call to time.sleep() to have the "parent" program wait "long enough"
for the "child" program to have done what it needs to do.  The problem
is you are only guessing how long is long enough, and will probably
be wrong enough of the time to be a problem.  

You need to work out some way to have the parent program wait for
a definite signal that the child is finished before moving forward.
That could be to use a subprocess invocation method which guarantees
the child will complete before it returns.  Or it could be that you
watch for an artifact created by the child, or wait for the child
process (or thread) to exit, or any of several other things.

Browse through the subprocess module for some hints of things you 
can try.


More information about the Tutor mailing list