Example #1
0
    def start(self):

        if not self.handlers or not self.bindings:
            raise ArgumentError(
                "To run a web applicaiton, we need to know the port and have at least one handler."
            )

        if self.loop and self.loop.running():
            return

        self.loop = IOLoop()

        th = threading.Thread(target=self._server_runner,
                              args=[self.handlers, self.bindings, self.loop])
        th.daemon = True
        th.start()
Example #2
0
class Base(object):

    def __init__(self, handlers = [], bindings = []):
        self.handlers = handlers
        self.bindings = list(bindings) # we will be rewriting port numbers when port 0 is used for "autoassign"
        self.loop = None

    def _server_runner(self, handlers, bindings, loop):
        raise NotImplementedError("Override _server_runner method in your sub-class.")

    def start(self):

        if not self.handlers or not self.bindings:
            raise ArgumentError("To run a web applicaiton, we need to know the port and have at least one handler.")

        if self.loop and self.loop.running():
            return

        self.loop = IOLoop()

        th = threading.Thread(
            target = self._server_runner
            , args = [
                self.handlers
                , self.bindings
                , self.loop
            ]
        )
        th.daemon = True
        th.start()

    def stop(self):
        logging.debug(HELLO + " stopping")
        # http://www.tornadoweb.org/documentation/ioloop.html
        # "It is safe to call this method from any thread at any time. 
        # Note that this is the only method in IOLoop that makes this guarantee;
        # all other interaction with the IOLoop must be done from that IOLoop’s
        # thread. add_callback() may be used to transfer control from other
        # threads to the IOLoop’s thread."
        loop = self.loop
        self.loop = None

        loop.add_callback(loop.stop)

        try:
            # it's unfortunate that Tornado people used "_" prefix for this.
            # This should be a public API property.
            loop._waker.wake()
        except:
            pass

    @property
    def is_running(self):
        return self.loop and self.loop.running()
Example #3
0
class Base(object):
    def __init__(self, handlers=[], bindings=[]):
        self.handlers = handlers
        self.bindings = list(
            bindings
        )  # we will be rewriting port numbers when port 0 is used for "autoassign"
        self.loop = None

    def _server_runner(self, handlers, bindings, loop):
        raise NotImplementedError(
            "Override _server_runner method in your sub-class.")

    def start(self):

        if not self.handlers or not self.bindings:
            raise ArgumentError(
                "To run a web applicaiton, we need to know the port and have at least one handler."
            )

        if self.loop and self.loop.running():
            return

        self.loop = IOLoop()

        th = threading.Thread(target=self._server_runner,
                              args=[self.handlers, self.bindings, self.loop])
        th.daemon = True
        th.start()

    def stop(self):
        logging.debug(HELLO + " stopping")
        # http://www.tornadoweb.org/documentation/ioloop.html
        # "It is safe to call this method from any thread at any time.
        # Note that this is the only method in IOLoop that makes this guarantee;
        # all other interaction with the IOLoop must be done from that IOLoop’s
        # thread. add_callback() may be used to transfer control from other
        # threads to the IOLoop’s thread."
        loop = self.loop
        self.loop = None

        loop.add_callback(loop.stop)

        try:
            # it's unfortunate that Tornado people used "_" prefix for this.
            # This should be a public API property.
            loop._waker.wake()
        except:
            pass

    @property
    def is_running(self):
        return self.loop and self.loop.running()
Example #4
0
    def start(self):

        if not self.handlers or not self.bindings:
            raise ArgumentError("To run a web applicaiton, we need to know the port and have at least one handler.")

        if self.loop and self.loop.running():
            return

        self.loop = IOLoop()

        th = threading.Thread(
            target = self._server_runner
            , args = [
                self.handlers
                , self.bindings
                , self.loop
            ]
        )
        th.daemon = True
        th.start()