Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (c) 2006- Facebook |
| 4 | # Distributed under the Thrift Software License |
| 5 | # |
| 6 | # See accompanying file LICENSE or visit the Thrift site at: |
| 7 | # http://developers.facebook.com/thrift/ |
| 8 | |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 9 | import sys |
| 10 | import traceback |
Mark Slee | 3c4d7fd | 2006-10-02 17:53:20 +0000 | [diff] [blame] | 11 | import threading |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 12 | import Queue |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 13 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 14 | from thrift.Thrift import TProcessor |
| 15 | from thrift.transport import TTransport |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 16 | from thrift.protocol import TBinaryProtocol |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 17 | |
| 18 | class TServer: |
| 19 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 20 | """Base interface for a server, which must have a serve method.""" |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 21 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 22 | """ 3 constructors for all servers: |
| 23 | 1) (processor, serverTransport) |
| 24 | 2) (processor, serverTransport, transportFactory, protocolFactory) |
| 25 | 3) (processor, serverTransport, |
| 26 | inputTransportFactory, outputTransportFactory, |
| 27 | inputProtocolFactory, outputProtocolFactory)""" |
| 28 | def __init__(self, *args): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 29 | if (len(args) == 2): |
| 30 | self.__initArgs__(args[0], args[1], |
| 31 | TTransport.TTransportFactoryBase(), |
| 32 | TTransport.TTransportFactoryBase(), |
| 33 | TBinaryProtocol.TBinaryProtocolFactory(), |
| 34 | TBinaryProtocol.TBinaryProtocolFactory()) |
| 35 | elif (len(args) == 4): |
| 36 | self.__initArgs__(args[0], args[1], args[2], args[2], args[3], args[3]) |
| 37 | elif (len(args) == 6): |
| 38 | self.__initArgs__(args[0], args[1], args[2], args[3], args[4], args[5]) |
| 39 | |
| 40 | def __initArgs__(self, processor, serverTransport, |
| 41 | inputTransportFactory, outputTransportFactory, |
| 42 | inputProtocolFactory, outputProtocolFactory): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 43 | self.processor = processor |
| 44 | self.serverTransport = serverTransport |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 45 | self.inputTransportFactory = inputTransportFactory |
| 46 | self.outputTransportFactory = outputTransportFactory |
| 47 | self.inputProtocolFactory = inputProtocolFactory |
| 48 | self.outputProtocolFactory = outputProtocolFactory |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 49 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 50 | def serve(self): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 51 | pass |
| 52 | |
| 53 | class TSimpleServer(TServer): |
| 54 | |
| 55 | """Simple single-threaded server that just pumps around one transport.""" |
| 56 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 57 | def __init__(self, *args): |
| 58 | TServer.__init__(self, *args) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 59 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 60 | def serve(self): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 61 | self.serverTransport.listen() |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 62 | while True: |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 63 | client = self.serverTransport.accept() |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 64 | itrans = self.inputTransportFactory.getTransport(client) |
| 65 | otrans = self.outputTransportFactory.getTransport(client) |
| 66 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
Mark Slee | fb84b2b | 2007-02-20 03:37:28 +0000 | [diff] [blame] | 67 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 68 | try: |
| 69 | while True: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 70 | self.processor.process(iprot, oprot) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 71 | except TTransport.TTransportException, tx: |
| 72 | pass |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 73 | except Exception, x: |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 74 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 75 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 76 | itrans.close() |
| 77 | otrans.close() |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 78 | |
| 79 | class TThreadedServer(TServer): |
| 80 | |
| 81 | """Threaded server that spawns a new thread per each connection.""" |
| 82 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 83 | def __init__(self, *args): |
| 84 | TServer.__init__(self, *args) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 85 | |
| 86 | def serve(self): |
| 87 | self.serverTransport.listen() |
| 88 | while True: |
| 89 | try: |
| 90 | client = self.serverTransport.accept() |
| 91 | t = threading.Thread(target = self.handle, args=(client,)) |
| 92 | t.start() |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 93 | except KeyboardInterrupt: |
| 94 | raise |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 95 | except Exception, x: |
| 96 | print '%s, %s, %s,' % (type(x), x, traceback.format_exc()) |
| 97 | |
| 98 | def handle(self, client): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 99 | itrans = self.inputTransportFactory.getTransport(client) |
| 100 | otrans = self.outputTransportFactory.getTransport(client) |
| 101 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
Mark Slee | fb84b2b | 2007-02-20 03:37:28 +0000 | [diff] [blame] | 102 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 103 | try: |
| 104 | while True: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 105 | self.processor.process(iprot, oprot) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 106 | except TTransport.TTransportException, tx: |
| 107 | pass |
| 108 | except Exception, x: |
| 109 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 110 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 111 | itrans.close() |
| 112 | otrans.close() |
| 113 | |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 114 | class TThreadPoolServer(TServer): |
| 115 | |
| 116 | """Server with a fixed size pool of threads which service requests.""" |
| 117 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 118 | def __init__(self, *args): |
| 119 | TServer.__init__(self, *args) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 120 | self.clients = Queue.Queue() |
| 121 | self.threads = 10 |
| 122 | |
Mark Slee | 4ce787f | 2006-10-24 18:54:06 +0000 | [diff] [blame] | 123 | def setNumThreads(self, num): |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 124 | """Set the number of worker threads that should be created""" |
| 125 | self.threads = num |
| 126 | |
| 127 | def serveThread(self): |
| 128 | """Loop around getting clients from the shared queue and process them.""" |
| 129 | while True: |
| 130 | try: |
Mark Slee | 9a695ba | 2006-10-24 18:55:36 +0000 | [diff] [blame] | 131 | client = self.clients.get() |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 132 | self.serveClient(client) |
| 133 | except Exception, x: |
| 134 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 135 | |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 136 | def serveClient(self, client): |
| 137 | """Process input/output from a client for as long as possible""" |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 138 | itrans = self.inputTransportFactory.getTransport(client) |
| 139 | otrans = self.outputTransportFactory.getTransport(client) |
| 140 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
Mark Slee | 04342d8 | 2007-02-20 03:41:35 +0000 | [diff] [blame] | 141 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 142 | try: |
| 143 | while True: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 144 | self.processor.process(iprot, oprot) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 145 | except TTransport.TTransportException, tx: |
| 146 | pass |
| 147 | except Exception, x: |
| 148 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |
| 149 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 150 | itrans.close() |
| 151 | otrans.close() |
| 152 | |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 153 | def serve(self): |
| 154 | """Start a fixed number of worker threads and put client into a queue""" |
| 155 | for i in range(self.threads): |
| 156 | try: |
| 157 | t = threading.Thread(target = self.serveThread) |
| 158 | t.start() |
| 159 | except Exception, x: |
| 160 | print '%s, %s, %s,' % (type(x), x, traceback.format_exc()) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 161 | |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 162 | # Pump the socket for clients |
| 163 | self.serverTransport.listen() |
| 164 | while True: |
| 165 | try: |
| 166 | client = self.serverTransport.accept() |
| 167 | self.clients.put(client) |
| 168 | except Exception, x: |
| 169 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |