00001 import sys 00002 import time 00003 from threading import Thread 00004 import threading 00005 00006 __copyright__ = """\ 00007 (c). Copyright 1990-2008, Vyper Logix Corp., All Rights Reserved. 00008 00009 Published under Creative Commons License 00010 (http://creativecommons.org/licenses/by-nc/3.0/) 00011 restricted to non-commercial educational use only., 00012 00013 See also: http://www.VyperLogix.com and http://www.pypi.info for details. 00014 00015 THE AUTHOR VYPER LOGIX CORP DISCLAIMS ALL WARRANTIES WITH REGARD TO 00016 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 00017 FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, 00018 INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 00019 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00020 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00021 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE ! 00022 00023 USE AT YOUR OWN RISK. 00024 """ 00025 00026 def threaded(func): 00027 def proxy(*args, **kwargs): 00028 thread = Thread(target=func, args=args, kwargs=kwargs) 00029 thread.start() 00030 return thread 00031 return proxy 00032 00033 from Queue import Queue 00034 00035 class ThreadQueue(Queue): 00036 def __init__(self, maxsize, isDaemon=False): 00037 self.__stopevent = threading.Event() 00038 assert maxsize > 0, 'maxsize > 0 required for ThreadQueue class' 00039 Queue.__init__(self, maxsize) 00040 for i in xrange(maxsize): 00041 thread = Thread(target = self._worker) 00042 thread.setDaemon(isDaemon) 00043 thread.start() 00044 00045 def getIsRunning(self): 00046 return not self.__stopevent.isSet() 00047 00048 def setIsRunning(self,isRunning): 00049 if (not isRunning): 00050 self.__stopevent.set() 00051 00052 def _worker(self): 00053 while not self.__stopevent.isSet(): 00054 if (not self.isRunning): 00055 break 00056 try: 00057 func, args, kwargs = self.get() 00058 func(*args, **kwargs) 00059 except Exception, details: 00060 import traceback 00061 print >>sys.stderr, '(%s._worker).Error :: "%s".' % (self.__class__,str(details)) 00062 print >>sys.stderr, traceback.format_exc() 00063 self.task_done() 00064 self.join() 00065 raise 00066 else: 00067 self.task_done() 00068 00069 def addJob(self, func, *args, **kwargs): 00070 self.put((func, args, kwargs)) 00071 00072 def __enter__(self): 00073 pass 00074 00075 def __exit__(self, exc_type, exc_value, traceback): 00076 self.__stopevent.set() 00077 self.join() 00078 00079 isRunning = property(getIsRunning, setIsRunning) 00080 00081 def threadify(threadQ): 00082 assert threadQ.__class__ in [ThreadQueue], 'threadify decorator requires a ThreadQueue or Queue object instance, use Queue when threading is not required.' 00083 def decorator(func): 00084 def proxy(*args, **kwargs): 00085 threadQ.put((func, args, kwargs)) 00086 return threadQ 00087 return proxy 00088 return decorator 00089 00090 if (__name__ == '__main__'): 00091 import sys 00092 print >>sys.stdout, __copyright__ 00093 print >>sys.stderr, __copyright__ 00094 00095
© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved; If you reference this document or any part of this document you must use the citation verbatim (including the link) "© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved."
Notice: This source code contained in this document is NOT open source and is NOT being distributed as open source.
122,241 lines of code and growing...