How to get success/failure in case of thread

Anand Pillai pythonguy at Hotpop.com
Tue Jul 29 08:42:26 EDT 2003


Aahz has given you the solution in his post. Either follow
it by setting an instance in your Thread derived object and
retrieve it by a 'get' method. Otherwise put the result in
a Queue and 'get' it from there as suggested by Skip.

 You cannot go forward much if you use the methods in the
'thread' module since they dont give you the fine control you
need.

 If code is what you want, here is a brief pseudo code
for the first solution. I am typing it online, so 
the indentation is bad. Dont try to run this anyway :-)

class mythread(threading.Thread):

    def __init__(self, name):
        threading.Thread.__init__(self, None, None, name)
        self._res = 0  # return value storage

    def run(self):
        """ Run this thread """
        self._res = self.doMyWork()

    def doMyWork(self):
        """ Do my work (return an integer) """
        # Add your thread's main code here
        ...
        ...        
        ...
        return val

    def getResult(self):
        """ Return the result of the thread execution """
        return self._res

     
A client of this thread object can always get the result of the
thread's work by calling the getResult() method, i.e before he
deletes the thread's object.

class threadClient:

      def __init__(self):
          ...
      
      def myop(self):
          t = mythread('Worker')
          t.start()
          t.join() # Block till thread finishes
          
          ret = t.getResult()
          # Take action based on 'ret' value

The Queue implementation is similar but only that you wont have
the getResult() method in the thread class but in the 'Queue' derived
class.

HTH.

~Anand


vivek at cs.unipune.ernet.in wrote in message news:<mailman.1059467622.12587.python-list at python.org>...
> On Mon, Jul 28, 2003 at 08:51:27AM -0500, Skip Montanaro wrote:
> > 
> >     vivek>   import thread
> >     vivek>   thread.start_new_thread(my_func,(args,))
> > 
> >     vivek> here my_func will return true/1 on success and false/0 on
> >     vivek> failure. How can I check whether the function failed/succeeded
> >     vivek> ???
> > 
> > Have the function (or a wrapper function) put() the return value on a Queue
> > object, then get() it from there.
> >
> 
> THX a lot..
> 
> But I think it will not work in case if I want to return some value from the 
> function. 
> 
> Like currently I am trying to implement three tier application using python.
> 
> I mean a web server that will serve the http request, depending on the request
> it will create an XML string and will pass it to another tier running 
> XMLRPCServer. On this tier the XML string will be parsed and will take actions 
> accordingly ( like insertion in database etc.) Now I want the SimpleXMLRPCServer
> to create a new thread for the request that will process the particular request
> and will return the success/failure message. 
> 
> How can I do that with the help of queue object?? 
> 
> TIA and kind Regards
> Vivek Kumar




More information about the Python-list mailing list