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 |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 22 | logger = logging.getLogger(__name__) |
| 23 | |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 24 | from multiprocessing import Process, Value, Condition, reduction |
| 25 | |
| 26 | from TServer import TServer |
| 27 | from thrift.transport.TTransport import TTransportException |
| 28 | |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 29 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 30 | class TProcessPoolServer(TServer): |
| 31 | """Server with a fixed size pool of worker subprocesses to service requests |
| 32 | |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 33 | Note that if you need shared state between the handlers - it's up to you! |
| 34 | Written by Dvir Volk, doat.com |
| 35 | """ |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 36 | def __init__(self, *args): |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 37 | TServer.__init__(self, *args) |
| 38 | self.numWorkers = 10 |
| 39 | self.workers = [] |
| 40 | self.isRunning = Value('b', False) |
| 41 | self.stopCondition = Condition() |
| 42 | self.postForkCallback = None |
| 43 | |
| 44 | def setPostForkCallback(self, callback): |
| 45 | if not callable(callback): |
| 46 | raise TypeError("This is not a callback!") |
| 47 | self.postForkCallback = callback |
| 48 | |
| 49 | def setNumWorkers(self, num): |
| 50 | """Set the number of worker threads that should be created""" |
| 51 | self.numWorkers = num |
| 52 | |
| 53 | def workerProcess(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 54 | """Loop getting clients from the shared queue and process them""" |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 55 | if self.postForkCallback: |
| 56 | self.postForkCallback() |
| 57 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 58 | while self.isRunning.value: |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 59 | try: |
| 60 | client = self.serverTransport.accept() |
Roger Meier | ab2793a | 2014-04-21 21:20:00 +0200 | [diff] [blame] | 61 | if not client: |
| 62 | continue |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 63 | self.serveClient(client) |
| 64 | except (KeyboardInterrupt, SystemExit): |
| 65 | return 0 |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame] | 66 | except Exception, x: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 67 | logger.exception(x) |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 68 | |
| 69 | def serveClient(self, client): |
| 70 | """Process input/output from a client for as long as possible""" |
| 71 | itrans = self.inputTransportFactory.getTransport(client) |
| 72 | otrans = self.outputTransportFactory.getTransport(client) |
| 73 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
| 74 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
| 75 | |
| 76 | try: |
| 77 | while True: |
| 78 | self.processor.process(iprot, oprot) |
| 79 | except TTransportException, tx: |
| 80 | pass |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame] | 81 | except Exception, x: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 82 | logger.exception(x) |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 83 | |
| 84 | itrans.close() |
| 85 | otrans.close() |
| 86 | |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 87 | def serve(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 88 | """Start workers and put into queue""" |
| 89 | # 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] | 90 | self.isRunning.value = True |
| 91 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 92 | # first bind and listen to the port |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 93 | self.serverTransport.listen() |
| 94 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 95 | # fork the children |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 96 | for i in range(self.numWorkers): |
| 97 | try: |
| 98 | w = Process(target=self.workerProcess) |
| 99 | w.daemon = True |
| 100 | w.start() |
| 101 | self.workers.append(w) |
| 102 | except Exception, x: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 103 | logger.exception(x) |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 104 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 105 | # wait until the condition is set by stop() |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 106 | while True: |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 107 | self.stopCondition.acquire() |
| 108 | try: |
| 109 | self.stopCondition.wait() |
| 110 | break |
| 111 | except (SystemExit, KeyboardInterrupt): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 112 | break |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame] | 113 | except Exception, x: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 114 | logger.exception(x) |
Bryan Duxbury | a48b7d6 | 2011-03-09 18:05:58 +0000 | [diff] [blame] | 115 | |
| 116 | self.isRunning.value = False |
| 117 | |
| 118 | def stop(self): |
| 119 | self.isRunning.value = False |
| 120 | self.stopCondition.acquire() |
| 121 | self.stopCondition.notify() |
| 122 | self.stopCondition.release() |