Patrick Smith wrote:
> Hi,
> Thanks for your reply.
>
> <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > [Re: cancelling a worker thread from the GUI thread]
> >
> > Have the main thread set a flag telling the worker thread to exit, and
> > have the worker thread check that periodically when it knows it's in a
> > safe state to exit.
> >
>
> This would work, unfortunately, the thread that it spawns calls a function
> in a loop, that function has an incredibly long run-time, on the order of
> minutes (possibly hours depending on the input), and that function, its self
> is multithreaded.
> This means that the worker thread could only check the flag after each
> completion of this long-running function.
>
> Given that this is the situation, is it possible to do what I mentioned
> above?  Or does the long running function prevent any nice way of doing
> this?

Well, the problem is that you can't simply kill a thread--it shares
memory with other threads that it could be leaving in an inconsistent
state.  Imagine that it was, say, holding a lock when it was forceably
killed.  Now any other thread that tries to acquire that lock will
block forever.

You really do need the thread's cooperation so that it only exits when
everything is in a kosher state.  Can you dig into the incredibly long
function and change it to do the flag-checking and exiting safely?

I second the notion to consider using subprocesses instead of threads;
that's almost always a good idea unless you're really sharing a lot of
complex data structures.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to