Esempio n. 1
0
class WebHandler(tornado.web.RequestHandler):

    __threadPool = getGlobalThreadPool()
    __disetConfig = ThreadConfig()
    __log = False

    #Auth requirements
    AUTH_PROPS = None
    #Location of the handler in the URL
    LOCATION = ""
    #URL Schema with holders to generate handler urls
    URLSCHEMA = ""
    #RE to extract group and setup
    PATH_RE = ""

    #Helper function to create threaded gen.Tasks with automatic callback and execption handling
    @classmethod
    def threadTask(cls, method, *args, **kwargs):
        """
    Helper method to generate a gen.Task and automatically call the callback when the real
    method ends. THIS IS SPARTAAAAAAAAAA
    """
        #Save the task to access the runner
        genTask = False

        #This runs in the separate thread, calls the callback on finish and takes into account exceptions
        def cbMethod(*cargs, **ckwargs):
            cb = ckwargs.pop('callback')
            method = cargs[0]
            disetConf = cargs[1]
            cargs = cargs[2]
            cls.__disetConfig.load(disetConf)
            ioloop = tornado.ioloop.IOLoop.instance()
            try:
                result = method(*cargs, **ckwargs)
                ioloop.add_callback(functools.partial(cb, result))
            except Exception, excp:
                exc_info = sys.exc_info()
                ioloop.add_callback(
                    lambda: genTask.runner.handle_exception(*exc_info))

        #Put the task in the thread :)
        def threadJob(tmethod, *targs, **tkwargs):
            tkwargs['callback'] = tornado.stack_context.wrap(
                tkwargs['callback'])
            targs = (tmethod, cls.__disetConfig.dump(), targs)
            cls.__threadPool.generateJobAndQueueIt(cbMethod,
                                                   args=targs,
                                                   kwargs=tkwargs)

        #Return a YieldPoint
        genTask = tornado.gen.Task(threadJob, method, *args, **kwargs)
        return genTask
Esempio n. 2
0
 def __init__(self, name, transportPool=None, threadPool=None):
     self.__name = name
     self.__messageTransports = {}
     self.__msgCounter = 0
     self.__msgCounterLock = threading.Lock()
     self.__responseCallbacks = {}
     self.__msgInTransport = {}
     self.__listenPersistConn = False
     self.__useMessageObjects = True
     self.__callbacksLock = threading.Condition()
     self.__trInOutLock = threading.Lock()
     self.__msgFactory = MessageFactory()
     self.__log = gLogger.getSubLogger("MSGBRK")
     if not transportPool:
         transportPool = getGlobalTransportPool()
     self.__trPool = transportPool
     if not threadPool:
         threadPool = getGlobalThreadPool()
     self.__threadPool = threadPool
     self.__listeningForMessages = False
Esempio n. 3
0
 def __init__(self, name, transportPool=None, threadPool=None):
     self.__name = name
     self.__messageTransports = {}
     self.__msgCounter = 0
     self.__msgCounterLock = threading.Lock()
     self.__responseCallbacks = {}
     self.__msgInTransport = {}
     self.__listenPersistConn = False
     self.__useMessageObjects = True
     self.__callbacksLock = threading.Condition()
     self.__trInOutLock = threading.Lock()
     self.__msgFactory = MessageFactory()
     self.__log = gLogger.getSubLogger("MSGBRK")
     if not transportPool:
         transportPool = getGlobalTransportPool()
     self.__trPool = transportPool
     if not threadPool:
         threadPool = getGlobalThreadPool()
     self.__threadPool = threadPool
     self.__listeningForMessages = False