-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_server.py
More file actions
37 lines (28 loc) · 878 Bytes
/
Copy pathhttp_server.py
File metadata and controls
37 lines (28 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import typing
from multiprocessing import Process
from tests.http_app import run_app
class HttpServerConfig(typing.NamedTuple):
host: str
port: int
ssl_certfile: str = None
ssl_keyfile: str = None
def to_dict(self):
d = {}
for key, val in self._asdict().items():
if val is not None:
d[key] = val
return d
class HttpServer:
def __init__(self, config: typing.Iterable[HttpServerConfig]):
self.config = config
self.workers = []
def run(self):
for cfg in self.config:
print(f'Starting web server on {cfg.host}:{cfg.port}...')
p = Process(target=run_app, kwargs=cfg.to_dict())
self.workers.append(p)
for p in self.workers:
p.start()
def shutdown(self):
for p in self.workers:
p.terminate()