Ejemplo n.º 1
0
class Server(object):
    def __init__(self, forsun):
        self.forsun = forsun
        self.server = None
        self.http_server = None
        self.thread = None

    def serve_thrift(self):
        handler = Handler(self.forsun)
        processor = Processor(handler)
        tfactory = TIOStreamTransportFactory()
        protocol = TBinaryProtocolAcceleratedFactory()

        bind_address = config.get("BIND_ADDRESS", "127.0.0.1")
        port = config.get("PORT", 6458)
        self.server = TTornadoServer(processor, tfactory, protocol)
        self.server.bind(port, bind_address)
        self.server.start(1)
        logging.info("starting server by %s:%s", bind_address, port)

    def serve_http(self):
        http_bind = config.get("HTTP_BIND")
        if not http_bind:
            return

        (address, port) = http_bind.split(":")
        application = Application(self.forsun, debug=False, autoreload=False)
        self.http_server = HTTPServer(application, xheaders=True)
        self.http_server.bind(int(port), address)
        self.http_server.start(1)
        logging.info("starting http server by %s", http_bind)

    def start(self, init_callback):
        def _():
            try:
                if asyncio is not None:
                    loop = asyncio.new_event_loop()
                    asyncio.set_event_loop(loop)

                self.serve_thrift()
                self.serve_http()
                ioloop = IOLoop.instance()
                ioloop.add_callback(init_callback)
                ioloop.start()
            except Exception as e:
                logging.error("server error: %s", e)
                self.forsun.read_event.set()
                timer.stop()

        self.thread = threading.Thread(target=_)
        self.thread.setDaemon(True)
        self.thread.start()

    def stop(self):
        IOLoop.current().add_callback(lambda: IOLoop.current().stop())
        logging.info("server stoping")
Ejemplo n.º 2
0
class Server(object):
    def __init__(self, forsun):
        self.forsun = forsun
        self.server = None
        self.http_server = None
        self.thread = None

    def serve_thrift(self):
        handler = Handler(self.forsun)
        processor = Processor(handler)
        tfactory = TIOStreamTransportFactory()
        protocol = TBinaryProtocolAcceleratedFactory()

        bind_address = config.get("BIND_ADDRESS", "127.0.0.1")
        port = config.get("PORT", 6458)
        self.server = TTornadoServer(processor, tfactory, protocol)
        self.server.bind(port, bind_address)
        self.server.start(1)
        logging.info("starting server by %s:%s", bind_address, port)

    def serve_http(self):
        http_bind = config.get("HTTP_BIND")
        if not http_bind:
            return

        (address, port) = http_bind.split(":")
        application = Application(self.forsun, debug=False, autoreload=False)
        self.http_server = HTTPServer(application, xheaders=True)
        self.http_server.bind(int(port), address)
        self.http_server.start(1)
        logging.info("starting http server by %s", http_bind)

    def start(self, init_callback):
        def _():
            try:
                if asyncio is not None:
                    loop = asyncio.new_event_loop()
                    asyncio.set_event_loop(loop)

                self.serve_thrift()
                self.serve_http()
                ioloop = IOLoop.instance()
                ioloop.add_callback(init_callback)
                ioloop.start()
            except Exception as e:
                logging.error("server error: %s", e)
                self.forsun.read_event.set()
                timer.stop()

        self.thread = threading.Thread(target=_)
        self.thread.setDaemon(True)
        self.thread.start()

    def stop(self):
        IOLoop.current().add_callback(lambda :IOLoop.current().stop())
        logging.info("server stoping")
Ejemplo n.º 3
0
    def serve(self):
        handler = Handler(self.translator, self.bpe)
        processor = Processor(handler)
        tfactory = TIOStreamTransportFactory()
        protocol = TBinaryProtocolFactory()

        server = TTornadoServer(processor, tfactory, protocol)
        server.bind(9095)
        server.start(1)  # MXNet requires we use a single thread, single process.
        IOLoop.instance().start()
Ejemplo n.º 4
0
class ThriftServer(object):
    def __init__(self):
        self.server = None
        self.manager = Manager(self)

    def serve(self):
        handler = Handler(self.manager)
        processor = Processor(handler)
        tfactory = TIOStreamTransportFactory()
        protocol = TBinaryProtocolAcceleratedFactory()
        self.server = TTornadoServer(processor, tfactory, protocol)
        self.server.processor._processMap["pull"] = process_pull

        bind_address = config.get("BIND_ADDRESS", "127.0.0.1")
        port = config.get("PORT", 6455)
        self.server.bind(port, bind_address)
        self.server.start(1)

        ioloop = IOLoop.instance()
        self.manager.start()
        logging.info("starting server by %s:%s", bind_address, port)
        ioloop.start()

    def get_manager(self):
        return self.manager

    def init(self):
        for collection_name in config.get("collections", []):
            collection = Collection(collection_name, self.manager)
            self.manager.register_collection(collection)
        self.manager.load_session()

    def start(self):
        try:
            self.init()
            self.server()
        except Exception as e:
            logging.error("server error: %s", e)

    def stop(self):
        IOLoop.current().add_callback(lambda: IOLoop.current().stop())
        logging.info("server stoping")
Ejemplo n.º 5
0
class ThriftServer(object):
    def __init__(self, forsun):
        self.forsun = forsun
        self.server = None
        self.thread = None

    def serve(self):
        handler = Handler(self.forsun)
        processor = Processor(handler)
        tfactory = TIOStreamTransportFactory()
        protocol = TBinaryProtocolAcceleratedFactory()

        bind_address = config.get("BIND_ADDRESS", "127.0.0.1")
        port = config.get("PORT", 6458)
        self.server = TTornadoServer(processor, tfactory, protocol)
        self.server.bind(port, bind_address)
        self.server.start(1)
        ioloop = IOLoop.instance()
        logging.info("starting server by %s:%s", bind_address, port)
        ioloop.start()

    def start(self):
        def _():
            try:
                self.serve()
            except Exception as e:
                logging.error("server error: %s", e)
                timer.stop()

        self.thread = threading.Thread(target=_)
        self.thread.setDaemon(True)
        self.thread.start()

    def stop(self):
        IOLoop.current().add_callback(lambda: IOLoop.current().stop())
        logging.info("server stoping")
Ejemplo n.º 6
0
sys.path.insert(
    0,
    os.path.abspath(os.path.dirname(os.path.abspath(
        os.path.dirname(__file__)))))
sys.path.append(
    os.path.abspath(
        os.path.abspath(os.path.dirname(__file__)) + os.sep + "gen-py"))

from thrift.protocol.TBinaryProtocol import TBinaryProtocolAcceleratedFactory
from torthrift.transport import TIOStreamTransportFactory
from torthrift.server import TTornadoServer
from example.Example import Processor
from tornado.ioloop import IOLoop
from torthrift import gen


class Handler(object):
    def add(self, a, b):
        return a + b


if __name__ == "__main__":
    handler = Handler()
    processor = Processor(handler)
    tfactory = TIOStreamTransportFactory()
    protocol = TBinaryProtocolAcceleratedFactory()

    server = TTornadoServer(processor, tfactory, protocol)
    server.bind(20000)
    server.start(0)
    IOLoop.instance().start()
Ejemplo n.º 7
0
#14-6-24
# create by: snower

import os
import sys

sys.path.append(os.path.abspath(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))))
sys.path.append(os.path.abspath(os.path.abspath(os.path.dirname(__file__))+os.sep+"gen-py.tornado"))

from torthrift.protocol import TBinaryProtocolFactory
from torthrift.transport import TIOStreamTransportFactory
from torthrift.server import TTornadoServer
from example.Example import Processor
from tornado.ioloop import IOLoop
from tornado import gen

class Handler(object):
    @gen.coroutine
    def add(self,a,b):
        raise gen.Return(a+b)

if __name__=="__main__":
    handler = Handler()
    processor = Processor(handler)
    tfactory = TIOStreamTransportFactory()
    protocol =TBinaryProtocolFactory()

    server = TTornadoServer(processor, tfactory, protocol)
    server.bind(20000)
    server.start(0)
    IOLoop.instance().start()