Terminate a thread that doesn't check for events

Chris Lambacher lambacck at computer.org
Tue Aug 2 12:15:48 EDT 2005


No.  On Linux a separate thread is a separate process with shared memory.  You
can send a signal to a particular process and catch that signal as an
indication that you need to terminate (perhapse something that is set up
before running your long running process so that it is generic).  In windows
threads are separate beasts from processes(almost) and there are functions for
terminating them (though I don't think gracefully).  Unfortunately I can't
comment on other platforms.

-Chris
  
On Tue, Aug 02, 2005 at 11:54:57AM -0400, Liu Shuai wrote:
> Thanks for the reply Chris. 
> 
> Are you suggesting running the task in a *separate* process instead of a
> separate *thread*? Because if that's the case, I will have to share data
> (and possible memory) between that new process and my "master" process
> since they depend on one other.
> 
> Yeah I will sure post it if I can find an elegant solution.
> 
> Thank you,
> LS
> 
> > -----Original Message-----
> > From: Chris Lambacher [mailto:lambacck at computer.org]
> > Sent: Tuesday, August 02, 2005 11:50 AM
> > To: Liu Shuai
> > Subject: Re: Terminate a thread that doesn't check for events
> > 
> > Hi,
> > 
> > There is no cross platform way to do this.  You need to periodically
> check
> > a
> > quit flag, or perform a platform specific action to terminate it.  On
> > Linux
> > you can send the process a signal.  On windows you will need to use
> the
> > win32all package.  If you decide to go the later route, maybe you
> could
> > encapsulate the thread termination code in a module and share it with
> the
> > Python community.
> > 
> > -Chris
> > 
> > On Tue, Aug 02, 2005 at 09:51:31AM -0400, Liu Shuai wrote:
> > >    Can someone please comment on this?
> > >
> > >
> > >
> > >
> ---------------------------------------------------------------------
> > -----
> > >
> > >    From: python-list-bounces+sliu=kaneva.com at python.org
> > >    [mailto:python-list-bounces+sliu=kaneva.com at python.org] On Behalf
> Of
> > Liu
> > >    Shuai
> > >    Sent: Monday, August 01, 2005 4:29 PM
> > >    To: python-list at python.org
> > >    Subject: Terminate a thread that doesn't check for events
> > >
> > >
> > >
> > >    Hi all,
> > >
> > >
> > >
> > >    I am aware that similar and probably same questions have been
> posted
> > many
> > >    times, but I was unable to find a solution after reading a dozen
> > threads
> > >    in the archive. So here it goes again.
> > >
> > >
> > >
> > >    I have a thread that does some heavy task (hash calculation, for
> > >    instance). I need a mechanism that works *across platforms* to
> stop
> > that
> > >    calculation if user decides to exit. How can I do that?
> > >
> > >    I can not do the standard "periodical checking" way since there
> is
> > only
> > >    one function/step call in my thread.
> > >
> > >
> > >
> > >    Here is a snippet of my thread classes to help illustrate the
> > problem:
> > >
> > >
> > >
> > >    class Dispatcher():
> > >
> > >        def __init__(self):
> > >
> > >                self.__worker = Worker()
> > >
> > >
> > >
> > >        def start(self):
> > >
> > >            self.__worker.start()
> > >
> > >
> > >
> > >        def stop():
> > >
> > >                #how do i stop the worker?
> > >
> > >
> > >
> > >    class Worker(threading.Thread):
> > >
> > >        def __init__(self):
> > >
> > >
> > >
> > >        def run(self):
> > >
> > >                oneTimeConsumingCall()
> > >
> > >
> > >
> > >
> > >
> > >    Thank you in advance for any suggestions/pointers.
> > >
> > >
> > >
> > >    LS
> > 
> > > --
> > > http://mail.python.org/mailman/listinfo/python-list
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list