post_threading.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 ########################################################################### 
00004 # This software is graciously provided by HumaRobotics 
00005 # under the Simplified BSD License on
00006 # github: git@www.humarobotics.com:baxter_tasker
00007 # HumaRobotics is a trademark of Generation Robots.
00008 # www.humarobotics.com 
00009 
00010 # Copyright (c) 2013, Generation Robots.
00011 # All rights reserved.
00012 # www.generationrobots.com
00013 #   
00014 # Redistribution and use in source and binary forms, with or without 
00015 # modification, are permitted provided that the following conditions are met:
00016 # 
00017 # 1. Redistributions of source code must retain the above copyright notice,
00018 #  this list of conditions and the following disclaimer.
00019 # 
00020 # 2. Redistributions in binary form must reproduce the above copyright notice,
00021 #  this list of conditions and the following disclaimer in the documentation 
00022 #  and/or other materials provided with the distribution.
00023 # 
00024 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
00026 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00027 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
00028 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00029 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
00030 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00031 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00032 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00033 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
00034 # THE POSSIBILITY OF SUCH DAMAGE. 
00035 # 
00036 # The views and conclusions contained in the software and documentation are 
00037 # those of the authors and should not be interpreted as representing official 
00038 # policies, either expressed or implied, of the FreeBSD Project.
00039 #
00040 #############################################################################
00041 
00042 """  
00043     Provides classes for quick threading of object methods
00044     
00045     In your class, just add this line to the __init__:
00046         self.post=Post(self)
00047         
00048     You can now call any object methods with object.post.method(args)
00049     The Thread object is returned.
00050     
00051 """
00052 
00053 
00054 from threading import Thread
00055 
00056 class PostThread(Thread):
00057     """ Object that manages the threaded execution of a given function """
00058     
00059     def __init__(self,func):
00060         """ Creates the thread object with the method to be called """
00061         Thread.__init__(self,)
00062         self.func=func
00063         self.isRunning=True
00064         self.result=None
00065         self.daemon=True
00066     
00067     def execute(self,*args,**kwargs):
00068         """ Store the method call arguments and start the thread, returns the thread object to the caller """
00069         self.args=args
00070         self.kwargs=kwargs
00071         self.start()
00072         return self
00073 
00074     def run(self):
00075         """ Thread execution, call the function with saved arguments, saves result and change flag at the end """
00076         self.result=self.func(*self.args,**self.kwargs)
00077         self.isRunning=False
00078 
00079     def kill(self):
00080         if self.isAlive():
00081             self._Thread__stop()
00082             
00083 class Post:
00084     """ Object that provides threaded calls to its parent object methods """
00085     def __init__(self,parent):
00086         self.parent=parent
00087     
00088     def __getattr__(self,attr):
00089         """ Find the method asked for in parent object, encapsulate in a PostThread object and send back pointer to execution function"""
00090         try:
00091             func=getattr(self.parent,attr)
00092             post_thread=PostThread(func)
00093             return post_thread.execute 
00094         except:
00095             raise Exception("ERROR: Post call on %s: method %s not found"%(str(self.parent),attr))
00096 
00097 
00098 if __name__=="__main__":
00099     class Dummy:
00100         def __init__(self):
00101             self.post=Post(self)
00102         
00103         def do(self,param):
00104             import time
00105             print "Doing... param="+str(param)
00106             time.sleep(2)
00107             print "Done"
00108             return param
00109             
00110 
00111     dummy=Dummy()
00112     dummy.do("direct1")
00113     dummy.post.do("post1")
00114     dummy.post.do("post2")
00115     t3=dummy.post.do("post3")
00116     t3.join()
00117     print t3.result
00118     print "Finished"
00119     


modbus
Author(s): Sven Bock
autogenerated on Thu Jun 6 2019 17:39:35