Skip to content

Commit 5635dcb

Browse files
committed
use proper config parser for handling cli flags. add worker count flag
1 parent 21806df commit 5635dcb

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

mystbin/backend/main.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import argparse
12
import json
23
import os
34
import pathlib
45
import sys
56
from typing import cast
67

8+
79
import uvicorn
810
from uvicorn.supervisors import Multiprocess
911

@@ -24,20 +26,19 @@ def get_config() -> dict[str, dict[str, int | str]]:
2426

2527
return data
2628

27-
28-
async def run_cli():
29-
pass
30-
31-
32-
def run_cli_with_workers():
33-
pass
34-
35-
3629
if __name__ == "__main__":
3730
cfg = get_config()
3831
port = cast(int, cfg["site"]["backend_port"])
39-
use_workers = "--no-workers" not in sys.argv and cfg["redis"]["use-redis"]
40-
use_cli = "--no-cli" not in sys.argv
32+
parser = argparse.ArgumentParser(prog="Mystbin")
33+
parser.add_argument("--no-workers", "-nw", action="store_true", default=False)
34+
parser.add_argument("--no-cli", "-nc", action="store_true", default=False)
35+
parser.add_argument("--workers", "-w", nargs=1, default=os.cpu_count() or 1)
36+
37+
ns = parser.parse_args(sys.argv[1:])
38+
39+
use_workers: bool = not ns.no_workers
40+
use_cli: bool = not ns.no_cli
41+
worker_count: int = ns.workers
4142
_cli_path = pathlib.Path(".nocli")
4243

4344
if not use_cli:
@@ -57,7 +58,7 @@ def run_cli_with_workers():
5758
server = uvicorn.Server(config)
5859

5960
if use_workers:
60-
config.workers = os.cpu_count() or 1
61+
config.workers = worker_count
6162
sock = config.bind_socket()
6263

6364
runner = Multiprocess(config, target=server.run, sockets=[sock])

0 commit comments

Comments
 (0)