Start a fixed number of worker threads and put client into a queue
(self)
| 84 | |
| 85 | |
| 86 | def serve(self): |
| 87 | """Start a fixed number of worker threads and put client into a queue""" |
| 88 | |
| 89 | #this is a shared state that can tell the workers to exit when set as false |
| 90 | self.isRunning.value = True |
| 91 | |
| 92 | #first bind and listen to the port |
| 93 | self.serverTransport.listen() |
| 94 | |
| 95 | #fork the children |
| 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: |
| 103 | logging.exception(x) |
| 104 | |
| 105 | #wait until the condition is set by stop() |
| 106 | |
| 107 | while True: |
| 108 | |
| 109 | self.stopCondition.acquire() |
| 110 | try: |
| 111 | self.stopCondition.wait() |
| 112 | break |
| 113 | except (SystemExit, KeyboardInterrupt): |
| 114 | break |
| 115 | except Exception, x: |
| 116 | logging.exception(x) |
| 117 | |
| 118 | self.isRunning.value = False |
| 119 | |
| 120 | def stop(self): |
| 121 | self.isRunning.value = False |