diff options
-rw-r--r-- | bitbake/lib/hashserv/__init__.py | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/bitbake/lib/hashserv/__init__.py b/bitbake/lib/hashserv/__init__.py index 544bc86b16..86aa7e9de6 100644 --- a/bitbake/lib/hashserv/__init__.py +++ b/bitbake/lib/hashserv/__init__.py | |||
@@ -10,6 +10,9 @@ import sqlite3 | |||
10 | import json | 10 | import json |
11 | import traceback | 11 | import traceback |
12 | import logging | 12 | import logging |
13 | import socketserver | ||
14 | import queue | ||
15 | import threading | ||
13 | from datetime import datetime | 16 | from datetime import datetime |
14 | 17 | ||
15 | logger = logging.getLogger('hashserv') | 18 | logger = logging.getLogger('hashserv') |
@@ -135,6 +138,41 @@ class HashEquivalenceServer(BaseHTTPRequestHandler): | |||
135 | self.send_error(400, explain=traceback.format_exc()) | 138 | self.send_error(400, explain=traceback.format_exc()) |
136 | return | 139 | return |
137 | 140 | ||
141 | class ThreadedHTTPServer(HTTPServer): | ||
142 | quit = False | ||
143 | |||
144 | def serve_forever(self): | ||
145 | self.requestqueue = queue.Queue() | ||
146 | self.handlerthread = threading.Thread(target=self.process_request_thread) | ||
147 | self.handlerthread.daemon = False | ||
148 | |||
149 | self.handlerthread.start() | ||
150 | super().serve_forever() | ||
151 | |||
152 | def process_request_thread(self): | ||
153 | while not self.quit: | ||
154 | try: | ||
155 | (request, client_address) = self.requestqueue.get(True) | ||
156 | except queue.Empty: | ||
157 | continue | ||
158 | if request is None: | ||
159 | continue | ||
160 | try: | ||
161 | self.finish_request(request, client_address) | ||
162 | except Exception: | ||
163 | self.handle_error(request, client_address) | ||
164 | finally: | ||
165 | self.shutdown_request(request) | ||
166 | |||
167 | def process_request(self, request, client_address): | ||
168 | self.requestqueue.put((request, client_address)) | ||
169 | |||
170 | def server_close(self): | ||
171 | super().server_close() | ||
172 | self.quit = True | ||
173 | self.requestqueue.put((None, None)) | ||
174 | self.handlerthread.join() | ||
175 | |||
138 | def create_server(addr, dbname, prefix=''): | 176 | def create_server(addr, dbname, prefix=''): |
139 | class Handler(HashEquivalenceServer): | 177 | class Handler(HashEquivalenceServer): |
140 | pass | 178 | pass |
@@ -171,4 +209,5 @@ def create_server(addr, dbname, prefix=''): | |||
171 | cursor.execute('CREATE INDEX IF NOT EXISTS outhash_lookup ON tasks_v2 (method, outhash)') | 209 | cursor.execute('CREATE INDEX IF NOT EXISTS outhash_lookup ON tasks_v2 (method, outhash)') |
172 | 210 | ||
173 | logger.info('Starting server on %s', addr) | 211 | logger.info('Starting server on %s', addr) |
174 | return HTTPServer(addr, Handler) | 212 | |
213 | return ThreadedHTTPServer(addr, Handler) | ||