Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 1 | import sys |
| 2 | import traceback |
Mark Slee | 3c4d7fd | 2006-10-02 17:53:20 +0000 | [diff] [blame] | 3 | import threading |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 4 | import Queue |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 5 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 6 | from thrift.Thrift import TProcessor |
| 7 | from thrift.transport import TTransport |
| 8 | |
| 9 | class TServer: |
| 10 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 11 | """Base interface for a server, which must have a serve method.""" |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 12 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 13 | def __init__(self, processor, serverTransport, transportFactory=None): |
| 14 | self.processor = processor |
| 15 | self.serverTransport = serverTransport |
| 16 | if transportFactory == None: |
| 17 | self.transportFactory = TTransport.TTransportFactoryBase() |
| 18 | else: |
| 19 | self.transportFactory = transportFactory |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 20 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 21 | def serve(self): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 22 | pass |
| 23 | |
| 24 | class TSimpleServer(TServer): |
| 25 | |
| 26 | """Simple single-threaded server that just pumps around one transport.""" |
| 27 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 28 | def __init__(self, processor, serverTransport, transportFactory=None): |
| 29 | TServer.__init__(self, processor, serverTransport, transportFactory) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 30 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 31 | def serve(self): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 32 | self.serverTransport.listen() |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 33 | while True: |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 34 | client = self.serverTransport.accept() |
| 35 | (input, output) = self.transportFactory.getIOTransports(client) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 36 | try: |
| 37 | while True: |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 38 | self.processor.process(input, output) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 39 | except TTransport.TTransportException, tx: |
| 40 | pass |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 41 | except Exception, x: |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 42 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 43 | |
| 44 | input.close() |
| 45 | output.close() |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 46 | |
| 47 | class TThreadedServer(TServer): |
| 48 | |
| 49 | """Threaded server that spawns a new thread per each connection.""" |
| 50 | |
| 51 | def __init__(self, processor, serverTransport, transportFactory=None): |
| 52 | TServer.__init__(self, processor, serverTransport, transportFactory) |
| 53 | |
| 54 | def serve(self): |
| 55 | self.serverTransport.listen() |
| 56 | while True: |
| 57 | try: |
| 58 | client = self.serverTransport.accept() |
| 59 | t = threading.Thread(target = self.handle, args=(client,)) |
| 60 | t.start() |
| 61 | except Exception, x: |
| 62 | print '%s, %s, %s,' % (type(x), x, traceback.format_exc()) |
| 63 | |
| 64 | def handle(self, client): |
| 65 | (input, output) = self.transportFactory.getIOTransports(client) |
| 66 | try: |
| 67 | while True: |
| 68 | self.processor.process(input, output) |
| 69 | except TTransport.TTransportException, tx: |
| 70 | pass |
| 71 | except Exception, x: |
| 72 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 73 | |
| 74 | class TThreadPoolServer(TServer): |
| 75 | |
| 76 | """Server with a fixed size pool of threads which service requests.""" |
| 77 | |
| 78 | def __init__(self, processor, serverTransport, transportFactory=None): |
| 79 | TServer.__init__(self, processor, serverTransport, transportFactory) |
| 80 | self.clients = Queue.Queue() |
| 81 | self.threads = 10 |
| 82 | |
Mark Slee | 4ce787f | 2006-10-24 18:54:06 +0000 | [diff] [blame] | 83 | def setNumThreads(self, num): |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 84 | """Set the number of worker threads that should be created""" |
| 85 | self.threads = num |
| 86 | |
| 87 | def serveThread(self): |
| 88 | """Loop around getting clients from the shared queue and process them.""" |
| 89 | while True: |
| 90 | try: |
Mark Slee | 9a695ba | 2006-10-24 18:55:36 +0000 | [diff] [blame^] | 91 | client = self.clients.get() |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 92 | self.serveClient(client) |
| 93 | except Exception, x: |
| 94 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |
| 95 | |
| 96 | def serveClient(self, client): |
| 97 | """Process input/output from a client for as long as possible""" |
| 98 | (input, output) = self.transportFactory.getIOTransports(client) |
| 99 | try: |
| 100 | while True: |
| 101 | self.processor.process(input, output) |
| 102 | except TTransport.TTransportException, tx: |
| 103 | pass |
| 104 | except Exception, x: |
| 105 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |
| 106 | |
| 107 | def serve(self): |
| 108 | """Start a fixed number of worker threads and put client into a queue""" |
| 109 | for i in range(self.threads): |
| 110 | try: |
| 111 | t = threading.Thread(target = self.serveThread) |
| 112 | t.start() |
| 113 | except Exception, x: |
| 114 | print '%s, %s, %s,' % (type(x), x, traceback.format_exc()) |
| 115 | |
| 116 | # Pump the socket for clients |
| 117 | self.serverTransport.listen() |
| 118 | while True: |
| 119 | try: |
| 120 | client = self.serverTransport.accept() |
| 121 | self.clients.put(client) |
| 122 | except Exception, x: |
| 123 | print '%s, %s, %s' % (type(x), x, traceback.format_exc()) |