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 | |
David Reiss | b04df76 | 2008-06-10 22:55:38 +0000 | [diff] [blame] | 9 | import logging |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 10 | import sys |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 11 | import os |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 12 | import traceback |
Mark Slee | 3c4d7fd | 2006-10-02 17:53:20 +0000 | [diff] [blame] | 13 | import threading |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 14 | import Queue |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 15 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 16 | from thrift.Thrift import TProcessor |
| 17 | from thrift.transport import TTransport |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 18 | from thrift.protocol import TBinaryProtocol |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 19 | |
| 20 | class TServer: |
| 21 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 22 | """Base interface for a server, which must have a serve method.""" |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 23 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 24 | """ 3 constructors for all servers: |
| 25 | 1) (processor, serverTransport) |
| 26 | 2) (processor, serverTransport, transportFactory, protocolFactory) |
| 27 | 3) (processor, serverTransport, |
| 28 | inputTransportFactory, outputTransportFactory, |
| 29 | inputProtocolFactory, outputProtocolFactory)""" |
| 30 | def __init__(self, *args): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 31 | if (len(args) == 2): |
| 32 | self.__initArgs__(args[0], args[1], |
| 33 | TTransport.TTransportFactoryBase(), |
| 34 | TTransport.TTransportFactoryBase(), |
| 35 | TBinaryProtocol.TBinaryProtocolFactory(), |
| 36 | TBinaryProtocol.TBinaryProtocolFactory()) |
| 37 | elif (len(args) == 4): |
| 38 | self.__initArgs__(args[0], args[1], args[2], args[2], args[3], args[3]) |
| 39 | elif (len(args) == 6): |
| 40 | self.__initArgs__(args[0], args[1], args[2], args[3], args[4], args[5]) |
| 41 | |
| 42 | def __initArgs__(self, processor, serverTransport, |
| 43 | inputTransportFactory, outputTransportFactory, |
| 44 | inputProtocolFactory, outputProtocolFactory): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 45 | self.processor = processor |
| 46 | self.serverTransport = serverTransport |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 47 | self.inputTransportFactory = inputTransportFactory |
| 48 | self.outputTransportFactory = outputTransportFactory |
| 49 | self.inputProtocolFactory = inputProtocolFactory |
| 50 | self.outputProtocolFactory = outputProtocolFactory |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 51 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 52 | def serve(self): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 53 | pass |
| 54 | |
| 55 | class TSimpleServer(TServer): |
| 56 | |
| 57 | """Simple single-threaded server that just pumps around one transport.""" |
| 58 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 59 | def __init__(self, *args): |
| 60 | TServer.__init__(self, *args) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 61 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 62 | def serve(self): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 63 | self.serverTransport.listen() |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 64 | while True: |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 65 | client = self.serverTransport.accept() |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 66 | itrans = self.inputTransportFactory.getTransport(client) |
| 67 | otrans = self.outputTransportFactory.getTransport(client) |
| 68 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
Mark Slee | fb84b2b | 2007-02-20 03:37:28 +0000 | [diff] [blame] | 69 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 70 | try: |
| 71 | while True: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 72 | self.processor.process(iprot, oprot) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 73 | except TTransport.TTransportException, tx: |
| 74 | pass |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 75 | except Exception, x: |
David Reiss | b04df76 | 2008-06-10 22:55:38 +0000 | [diff] [blame] | 76 | logging.exception(x) |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 77 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 78 | itrans.close() |
| 79 | otrans.close() |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 80 | |
| 81 | class TThreadedServer(TServer): |
| 82 | |
| 83 | """Threaded server that spawns a new thread per each connection.""" |
| 84 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 85 | def __init__(self, *args): |
| 86 | TServer.__init__(self, *args) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 87 | |
| 88 | def serve(self): |
| 89 | self.serverTransport.listen() |
| 90 | while True: |
| 91 | try: |
| 92 | client = self.serverTransport.accept() |
| 93 | t = threading.Thread(target = self.handle, args=(client,)) |
| 94 | t.start() |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 95 | except KeyboardInterrupt: |
| 96 | raise |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 97 | except Exception, x: |
David Reiss | b04df76 | 2008-06-10 22:55:38 +0000 | [diff] [blame] | 98 | logging.exception(x) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 99 | |
| 100 | def handle(self, client): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 101 | itrans = self.inputTransportFactory.getTransport(client) |
| 102 | otrans = self.outputTransportFactory.getTransport(client) |
| 103 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
Mark Slee | fb84b2b | 2007-02-20 03:37:28 +0000 | [diff] [blame] | 104 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 105 | try: |
| 106 | while True: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 107 | self.processor.process(iprot, oprot) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 108 | except TTransport.TTransportException, tx: |
| 109 | pass |
| 110 | except Exception, x: |
David Reiss | b04df76 | 2008-06-10 22:55:38 +0000 | [diff] [blame] | 111 | logging.exception(x) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 112 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 113 | itrans.close() |
| 114 | otrans.close() |
| 115 | |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 116 | class TThreadPoolServer(TServer): |
| 117 | |
| 118 | """Server with a fixed size pool of threads which service requests.""" |
| 119 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 120 | def __init__(self, *args): |
| 121 | TServer.__init__(self, *args) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 122 | self.clients = Queue.Queue() |
| 123 | self.threads = 10 |
| 124 | |
Mark Slee | 4ce787f | 2006-10-24 18:54:06 +0000 | [diff] [blame] | 125 | def setNumThreads(self, num): |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 126 | """Set the number of worker threads that should be created""" |
| 127 | self.threads = num |
| 128 | |
| 129 | def serveThread(self): |
| 130 | """Loop around getting clients from the shared queue and process them.""" |
| 131 | while True: |
| 132 | try: |
Mark Slee | 9a695ba | 2006-10-24 18:55:36 +0000 | [diff] [blame] | 133 | client = self.clients.get() |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 134 | self.serveClient(client) |
| 135 | except Exception, x: |
David Reiss | b04df76 | 2008-06-10 22:55:38 +0000 | [diff] [blame] | 136 | logging.exception(x) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 137 | |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 138 | def serveClient(self, client): |
| 139 | """Process input/output from a client for as long as possible""" |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 140 | itrans = self.inputTransportFactory.getTransport(client) |
| 141 | otrans = self.outputTransportFactory.getTransport(client) |
| 142 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
Mark Slee | 04342d8 | 2007-02-20 03:41:35 +0000 | [diff] [blame] | 143 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 144 | try: |
| 145 | while True: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 146 | self.processor.process(iprot, oprot) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 147 | except TTransport.TTransportException, tx: |
| 148 | pass |
| 149 | except Exception, x: |
David Reiss | b04df76 | 2008-06-10 22:55:38 +0000 | [diff] [blame] | 150 | logging.exception(x) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 151 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 152 | itrans.close() |
| 153 | otrans.close() |
| 154 | |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 155 | def serve(self): |
| 156 | """Start a fixed number of worker threads and put client into a queue""" |
| 157 | for i in range(self.threads): |
| 158 | try: |
| 159 | t = threading.Thread(target = self.serveThread) |
| 160 | t.start() |
| 161 | except Exception, x: |
David Reiss | b04df76 | 2008-06-10 22:55:38 +0000 | [diff] [blame] | 162 | logging.exception(x) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 163 | |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 164 | # Pump the socket for clients |
| 165 | self.serverTransport.listen() |
| 166 | while True: |
| 167 | try: |
| 168 | client = self.serverTransport.accept() |
| 169 | self.clients.put(client) |
| 170 | except Exception, x: |
David Reiss | b04df76 | 2008-06-10 22:55:38 +0000 | [diff] [blame] | 171 | logging.exception(x) |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 172 | |
| 173 | |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 174 | class TForkingServer(TServer): |
| 175 | |
| 176 | """A Thrift server that forks a new process for each request""" |
| 177 | """ |
| 178 | This is more scalable than the threaded server as it does not cause |
| 179 | GIL contention. |
| 180 | |
| 181 | Note that this has different semantics from the threading server. |
| 182 | Specifically, updates to shared variables will no longer be shared. |
| 183 | It will also not work on windows. |
| 184 | |
| 185 | This code is heavily inspired by SocketServer.ForkingMixIn in the |
| 186 | Python stdlib. |
| 187 | """ |
| 188 | |
| 189 | def __init__(self, *args): |
| 190 | TServer.__init__(self, *args) |
| 191 | self.children = [] |
| 192 | |
| 193 | def serve(self): |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 194 | def try_close(file): |
| 195 | try: |
| 196 | file.close() |
| 197 | except IOError, e: |
David Reiss | b04df76 | 2008-06-10 22:55:38 +0000 | [diff] [blame] | 198 | logging.warning(e, exc_info=True) |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 199 | |
| 200 | |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 201 | self.serverTransport.listen() |
| 202 | while True: |
| 203 | client = self.serverTransport.accept() |
| 204 | try: |
| 205 | pid = os.fork() |
| 206 | |
| 207 | if pid: # parent |
| 208 | # add before collect, otherwise you race w/ waitpid |
| 209 | self.children.append(pid) |
| 210 | self.collect_children() |
| 211 | |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 212 | # Parent must close socket or the connection may not get |
| 213 | # closed promptly |
| 214 | itrans = self.inputTransportFactory.getTransport(client) |
| 215 | otrans = self.outputTransportFactory.getTransport(client) |
| 216 | try_close(itrans) |
| 217 | try_close(otrans) |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 218 | else: |
| 219 | itrans = self.inputTransportFactory.getTransport(client) |
| 220 | otrans = self.outputTransportFactory.getTransport(client) |
| 221 | |
| 222 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
| 223 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
| 224 | |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 225 | ecode = 0 |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 226 | try: |
Kevin Clark | 1e0744d | 2008-06-24 20:46:32 +0000 | [diff] [blame^] | 227 | try: |
| 228 | while True: |
| 229 | self.processor.process(iprot, oprot) |
| 230 | except TTransport.TTransportException, tx: |
| 231 | pass |
| 232 | except Exception, e: |
| 233 | logging.exception(e) |
| 234 | ecode = 1 |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 235 | finally: |
| 236 | try_close(itrans) |
| 237 | try_close(otrans) |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 238 | |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 239 | os._exit(ecode) |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 240 | |
| 241 | except TTransport.TTransportException, tx: |
| 242 | pass |
| 243 | except Exception, x: |
David Reiss | b04df76 | 2008-06-10 22:55:38 +0000 | [diff] [blame] | 244 | logging.exception(x) |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 245 | |
| 246 | |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 247 | def collect_children(self): |
| 248 | while self.children: |
| 249 | try: |
| 250 | pid, status = os.waitpid(0, os.WNOHANG) |
| 251 | except os.error: |
| 252 | pid = None |
| 253 | |
| 254 | if pid: |
| 255 | self.children.remove(pid) |
| 256 | else: |
| 257 | break |
| 258 | |
| 259 | |