コード例 #1
0
ファイル: py3_test_server.py プロジェクト: xuleiming/fbthrift
def main():
    op = optparse.OptionParser(usage='%prog [options]', add_help_option=False)
    op.add_option('-p',
                  '--port',
                  action='store',
                  type='int',
                  dest='port',
                  default=1234,
                  help='The server port')
    op.add_option('-s',
                  '--servertype',
                  action='store',
                  type='string',
                  dest='servertype',
                  default='TNonblockingServer',
                  help='Type name of server')
    op.add_option('-w',
                  '--num_workers',
                  action='store',
                  type='int',
                  dest='workers',
                  default=4,
                  help='Number of worker processes/threads')
    op.add_option('-Q',
                  '--max_queue_size',
                  action='store',
                  type='int',
                  dest='max_queue_size',
                  default=0,
                  help='Max queue size, passed to TNonblockingServer')
    op.add_option('-h',
                  '--header',
                  action='store_true',
                  help='Use the generated ContextIface')
    op.add_option('-?',
                  '--help',
                  action='help',
                  help='Show this help message and exit')

    (options, args) = op.parse_args()
    if args:
        op.error('trailing arguments: ' + ' '.join(args))

    handler = LoadHandler()
    processor = LoadTest.Processor(handler)

    if options.header:
        pfactory = THeaderProtocolFactory(True, [
            CLIENT_TYPE.HEADER, CLIENT_TYPE.FRAMED_DEPRECATED,
            CLIENT_TYPE.UNFRAMED_DEPRECATED, CLIENT_TYPE.HTTP_SERVER
        ])
        if options.servertype == 'TCppServer':
            print('C++ ThriftServer, Header transport, backwards compatible '
                  'with all other types')
        elif options.servertype == 'TNonblockingServer':
            print('Header transport, backwards compatible with framed')
        else:
            print('Header transport, backwards compatible with ' +
                  'unframed, framed, http')
    else:
        if options.servertype == 'TCppServer':
            if not options.header:
                op.error('TCppServer cannot be used without header')
        elif options.servertype == 'TNonblockingServer':
            print('Framed transport')
        else:
            print('Unframed transport')
        pfactory = TBinaryProtocolAcceleratedFactory()

    if options.servertype == 'TCppServer':
        server = TCppServer.TCppServer(processor)
        server.setPort(options.port)
        print('Worker threads: ' + str(options.workers))
        server.setNumIOWorkerThreads(options.workers)
    else:
        transport = TSocket.TServerSocket(options.port)
        tfactory = TTransport.TBufferedTransportFactory()
        if options.servertype == "TNonblockingServer":
            server = TNonblockingServer.TNonblockingServer(
                processor,
                transport,
                pfactory,
                maxQueueSize=options.max_queue_size)
        elif options.servertype == "TProcessPoolServer":
            server = TProcessPoolServer.TProcessPoolServer(
                processor, transport, tfactory, pfactory)
            print('Worker processes: ' + str(options.workers))
            server.setNumWorkers(options.workers)
        else:
            ServerClass = getattr(TServer, options.servertype)
            server = ServerClass(processor, transport, tfactory, pfactory)

    print('Serving ' + options.servertype + ' requests on port %d...' %
          (options.port, ))
    server.serve()
コード例 #2
0
ファイル: py3_test_server.py プロジェクト: ktprime/fbthrift
def main():
    op = optparse.OptionParser(usage="%prog [options]", add_help_option=False)
    op.add_option(
        "-p",
        "--port",
        action="store",
        type="int",
        dest="port",
        default=1234,
        help="The server port",
    )
    op.add_option(
        "-s",
        "--servertype",
        action="store",
        type="string",
        dest="servertype",
        default="TCppServer",
        help="Type name of server",
    )
    op.add_option(
        "-w",
        "--num_workers",
        action="store",
        type="int",
        dest="workers",
        default=4,
        help="Number of worker processes/threads",
    )
    op.add_option(
        "-h",
        "--header",
        action="store_true",
        default=True,
        help="Use the generated ContextIface",
    )
    op.add_option("-?",
                  "--help",
                  action="help",
                  help="Show this help message and exit")

    (options, args) = op.parse_args()
    if args:
        op.error("trailing arguments: " + " ".join(args))

    handler = LoadHandler()
    processor = LoadTest.Processor(handler)

    if options.header:
        pfactory = THeaderProtocolFactory(
            True,
            [
                CLIENT_TYPE.HEADER,
                CLIENT_TYPE.FRAMED_DEPRECATED,
                CLIENT_TYPE.UNFRAMED_DEPRECATED,
                CLIENT_TYPE.HTTP_SERVER,
            ],
        )
        if options.servertype == "TCppServer":
            print("C++ ThriftServer, Header transport, backwards compatible "
                  "with all other types")
        else:
            print("Header transport, backwards compatible with " +
                  "unframed, framed, http")
    else:
        if options.servertype == "TCppServer":
            if not options.header:
                op.error("TCppServer cannot be used without header")
        else:
            print("Unframed transport")
        pfactory = TBinaryProtocolAcceleratedFactory()

    if options.servertype == "TCppServer":
        server = TCppServer.TCppServer(processor)
        server.setPort(options.port)
        print("Worker threads: " + str(options.workers))
        server.setNumIOWorkerThreads(options.workers)
    else:
        transport = TSocket.TServerSocket(options.port)
        tfactory = TTransport.TBufferedTransportFactory()
        ServerClass = getattr(TServer, options.servertype)
        server = ServerClass(processor, transport, tfactory, pfactory)

    print("Serving " + options.servertype + " requests on port %d..." %
          (options.port, ))
    server.serve()