Start workers and put into queue
(self)
| 91 | otrans.close() |
| 92 | |
| 93 | def serve(self): |
| 94 | """Start workers and put into queue""" |
| 95 | # this is a shared state that can tell the workers to exit when False |
| 96 | self.isRunning.value = True |
| 97 | |
| 98 | # first bind and listen to the port |
| 99 | self.serverTransport.listen() |
| 100 | |
| 101 | # fork the children |
| 102 | for i in range(self.numWorkers): |
| 103 | try: |
| 104 | w = Process(target=self.workerProcess) |
| 105 | w.daemon = True |
| 106 | w.start() |
| 107 | self.workers.append(w) |
| 108 | except Exception as x: |
| 109 | logger.exception(x) |
| 110 | |
| 111 | # wait until the condition is set by stop() |
| 112 | while True: |
| 113 | self.stopCondition.acquire() |
| 114 | try: |
| 115 | self.stopCondition.wait() |
| 116 | break |
| 117 | except (SystemExit, KeyboardInterrupt): |
| 118 | break |
| 119 | except Exception as x: |
| 120 | logger.exception(x) |
| 121 | |
| 122 | self.isRunning.value = False |
| 123 | |
| 124 | def stop(self): |
| 125 | self.isRunning.value = False |