python's threading has no "interrupt"?

Anand Pillai pythonguy at Hotpop.com
Thu Dec 4 02:16:28 EST 2003


Python has 2 thread APIs. The primitive 'thread' API and the more recent
'threading' API. 

The preferred way to do thread programming in Python is to derive your
own thread class from the 'Thread' class in the 'threading' module and
implement your own interrupt and terminate methods for the thread. The
API as such does not provide any such methods.

For example, to create a 'dumb' thread which runs till the
function passed as target to it finishes can be created as.

import threading

t=threading.Thread(None, target_func, None, None)
t.start()

whereas, a better way is ...

class MyThread(threading.Thread):

     def __init__(self):
         self.end_flag=False
         self.thread_suspend=False
         self.sleep_time=0.0
         self.thread_sleep=False

     def run(self):
 
         while not self.end_flag:
              // Optional sleep
              if self.thread_sleep:
                  time.sleep(self._sleeptime)
              // Optional suspend
              while self.thread_suspend:
                    time.sleep(1.0)
              self.target_func()

     def target_func(self):

          // Do your stuff here
          pass

     def terminate(self):
         """ Thread termination routine """

         self.end_flag = True

     def set_sleep(self, sleeptime):
         self.thread_sleep = True
         self._sleeptime = sleeptime

     def suspend_thread(self):
         self.thread_suspend=True
 
     def resume_thread(self):
         self.thread_suspend=False

The above thread class is a more intelligent thread which can be
terminated, suspended, resumed and forced to sleep for some fixed
time.

You can add more attributes to this class to make it richer, like
possibly adding signal handling routines.

rgds

-Anand Pillai

Jay O'Connor <joconnor at cybermesa.com> wrote in message news:<bqid0j$t2t$1 at reader2.nmix.net>...
> Dave Brueck wrote:
> 
> >What's your actual use case (IOW, what programming problem are you trying to
> >solve)? Maybe there is a Python solution that will work for you, but in order
> >to help you find it, people here will need to better understand what you're
> >trying to do.
> >
> >You've noticed that there isn't an identical construct for what you were doing
> >in Java, so it may be that the Python way will be a completely different
> >approach to the problem rather than just a direct conversion from Java to
> >Python syntax.
> >  
> >
> 
> 
> Well, I'll give an example from something I tried to do a few years ago 
> which I couldn't translate to Python, though I took a stab at it.
> 
> The language in question was a VM language with it's own internal 
> process model.  The process model was all inteneral to on eOS process to 
> it was a cooperative model of multi-threading
> 
> The application I wrote was a web server on the front of an application 
> so that web requests were all handled in the application.  IOW, the 
> application had a web server interface)
> 
> The language process model had both the ability to set process 
> priorities, as well as allow processes to sleep and cede control to 
> other processes.
> 
> When a web request would come in, the handling of the request would be 
> forked as a seperate process so that the server could accept the next 
> request.  Both the server process and the handling process(es) were at 
> the same priority level so in theory each process would run to 
> completion before allowing another process to run.  For this reason, 
> both the server process and the handling processes would issue a 'yield' 
> at periodic strategic points, allowing themselves to temporarily halt 
> and for the next process of equal priority that was waiting to run.   
> This allowed both the server to remain responsive and all handling 
> processes to run efficiently.
> 
> Behind all of this was a higher priority process running, but it 
> basically would sleep for several minutes (sleeping allowed lower 
> priority process, like the server process to run).  When the background 
> process would wake up, since it was a higher priority, it would 
> immediately take control.  It's main job was to check the list of 
> handling processes for any that had been running too long (long running 
> processes in a web server meant that something had gone wrong) and 
> terminate them (freeing up the process and socket resources, etc..).  
> Then it (the cleanup process) would go back to sleep and let the lower 
> priority processes run.
> 
> At one point I attempted to translate this all into Python, but the lack 
> of the ability to set process priorities, or for processes to yield 
> control to other processes, or an effective way to terminate proceses, 
> kept me from doing this.  The Python threading model seemed very 
> primitve compared to what I was used to in regards to how threads can be 
> controlled.
> 
> Rather than being a knock on Python, I would prefer to know how to do 
> all this, if it can be done.




More information about the Python-list mailing list