diff options
author | Craig Tiller <ctiller@google.com> | 2017-04-24 22:36:05 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-24 22:36:05 -0700 |
commit | b219338e5426ae6fc483d568432fcf3ca110a279 (patch) | |
tree | bcc9279734aed041904e0482d5cc099601a0fe69 /tools | |
parent | dc2e0843dc1f7b152b145e167ff2990742716a43 (diff) | |
parent | ec4952426976c60e33fb279aa36a1cd1f89bd938 (diff) |
Merge pull request #10822 from ctiller/threaded_port_server
Threaded port_server
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/run_tests/python_utils/port_server.py | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/tools/run_tests/python_utils/port_server.py b/tools/run_tests/python_utils/port_server.py index dbd32efc0e..522cbed9e1 100755 --- a/tools/run_tests/python_utils/port_server.py +++ b/tools/run_tests/python_utils/port_server.py @@ -33,18 +33,20 @@ from __future__ import print_function import argparse -from six.moves import BaseHTTPServer +from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler import hashlib import os import socket import sys import time +from SocketServer import ThreadingMixIn +import threading # increment this number whenever making a change to ensure that # the changes are picked up by running CI servers # note that all changes must be backwards compatible -_MY_VERSION = 9 +_MY_VERSION = 11 if len(sys.argv) == 2 and sys.argv[1] == 'dump_version': @@ -68,6 +70,7 @@ print('port server running on port %d' % args.port) pool = [] in_use = {} +mu = threading.Lock() def refill_pool(max_timeout, req): @@ -95,28 +98,33 @@ def refill_pool(max_timeout, req): def allocate_port(req): global pool global in_use + global mu + mu.acquire() max_timeout = 600 while not pool: refill_pool(max_timeout, req) if not pool: req.log_message("failed to find ports: retrying soon") + mu.release() time.sleep(1) + mu.acquire() max_timeout /= 2 port = pool[0] pool = pool[1:] in_use[port] = time.time() + mu.release() return port keep_running = True -class Handler(BaseHTTPServer.BaseHTTPRequestHandler): +class Handler(BaseHTTPRequestHandler): def setup(self): # If the client is unreachable for 5 seconds, close the connection self.timeout = 5 - BaseHTTPServer.BaseHTTPRequestHandler.setup(self) + BaseHTTPRequestHandler.setup(self) def do_GET(self): global keep_running @@ -158,12 +166,12 @@ class Handler(BaseHTTPServer.BaseHTTPRequestHandler): elif self.path == '/quitquitquit': self.send_response(200) self.end_headers() - keep_running = False + sys.exit(0) +class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): + """Handle requests in a separate thread""" -httpd = BaseHTTPServer.HTTPServer(('', args.port), Handler) -while keep_running: - httpd.handle_request() - sys.stderr.flush() -print('done') +httpd = ThreadedHTTPServer(('', args.port), Handler) +httpd.serve_forever() + |