Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
| 20 | |
| 21 | import logging |
| 22 | from multiprocessing import Process, Value, Condition, reduction |
| 23 | |
| 24 | from TServer import TServer |
| 25 | from thrift.transport.TTransport import TTransportException |
| 26 | |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 27 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 28 | class TProcessPoolServer(TServer): |
| 29 | """Server with a fixed size pool of worker subprocesses to service requests |
| 30 | |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 31 | Note that if you need shared state between the handlers - it's up to you! |
| 32 | Written by Dvir Volk, doat.com |
| 33 | """ |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 34 | def __init__(self, *args): |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 35 | TServer.__init__(self, *args) |
| 36 | self.numWorkers = 10 |
| 37 | self.workers = [] |
| 38 | self.isRunning = Value('b', False) |
| 39 | self.stopCondition = Condition() |
| 40 | self.postForkCallback = None |
| 41 | |
| 42 | def setPostForkCallback(self, callback): |
| 43 | if not callable(callback): |
| 44 | raise TypeError("This is not a callback!") |
| 45 | self.postForkCallback = callback |
| 46 | |
| 47 | def setNumWorkers(self, num): |
| 48 | """Set the number of worker threads that should be created""" |
| 49 | self.numWorkers = num |
| 50 | |
| 51 | def workerProcess(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 52 | """Loop getting clients from the shared queue and process them""" |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 53 | if self.postForkCallback: |
| 54 | self.postForkCallback() |
| 55 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 56 | while self.isRunning.value: |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 57 | try: |
| 58 | client = self.serverTransport.accept() |
| 59 | self.serveClient(client) |
| 60 | except (KeyboardInterrupt, SystemExit): |
| 61 | return 0 |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame^] | 62 | except Exception, x: |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 63 | logging.exception(x) |
| 64 | |
| 65 | def serveClient(self, client): |
| 66 | """Process input/output from a client for as long as possible""" |
| 67 | itrans = self.inputTransportFactory.getTransport(client) |
| 68 | otrans = self.outputTransportFactory.getTransport(client) |
| 69 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
| 70 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
| 71 | |
| 72 | try: |
| 73 | while True: |
| 74 | self.processor.process(iprot, oprot) |
| 75 | except TTransportException, tx: |
| 76 | pass |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame^] | 77 | except Exception, x: |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 78 | logging.exception(x) |
| 79 | |
| 80 | itrans.close() |
| 81 | otrans.close() |
| 82 | |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 83 | def serve(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 84 | """Start workers and put into queue""" |
| 85 | # this is a shared state that can tell the workers to exit when False |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 86 | self.isRunning.value = True |
| 87 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 88 | # first bind and listen to the port |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 89 | self.serverTransport.listen() |
| 90 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 91 | # fork the children |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 92 | for i in range(self.numWorkers): |
| 93 | try: |
| 94 | w = Process(target=self.workerProcess) |
| 95 | w.daemon = True |
| 96 | w.start() |
| 97 | self.workers.append(w) |
| 98 | except Exception, x: |
| 99 | logging.exception(x) |
| 100 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 101 | # wait until the condition is set by stop() |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 102 | while True: |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 103 | self.stopCondition.acquire() |
| 104 | try: |
| 105 | self.stopCondition.wait() |
| 106 | break |
| 107 | except (SystemExit, KeyboardInterrupt): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 108 | break |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame^] | 109 | except Exception, x: |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 110 | logging.exception(x) |
| 111 | |
| 112 | self.isRunning.value = False |
| 113 | |
| 114 | def stop(self): |
| 115 | self.isRunning.value = False |
| 116 | self.stopCondition.acquire() |
| 117 | self.stopCondition.notify() |
| 118 | self.stopCondition.release() |