Overview
ArgParser is the CLI argument parser for TorchServe that defines and processes command-line arguments for both the main torchserve server process (start, stop, version) and the backend model service worker processes. It is implemented as a single class with static methods that return configured argparse.ArgumentParser instances for each component.
Description
The arg_parser.py module (162 lines) provides a centralized argument parsing facility for TorchServe. The ArgParser class (lines 10-163) contains static methods that define the CLI interface for the two main TorchServe process types: the frontend server and the backend model service workers.
Key Responsibilities
- Server CLI Definition: Defines the
torchserve command-line interface with subcommands for --start, --stop, and --version
- Configuration Options: Exposes options for config file (
--ts-config), model store (--model-store), models (--models), log config (--log-config), and more
- Worker CLI Definition: Defines the backend worker argument interface with socket type, host, port, and metrics configuration
- Argument Extraction: Provides
extract_args() as a convenience wrapper to parse and return arguments
Code Reference
Source Location
Class Definition
class ArgParser:
"""
CLI argument parser for TorchServe. Lines 10-163.
Provides static methods that return argparse.ArgumentParser instances
configured for the TorchServe server and backend worker processes.
"""
@staticmethod
def ts_parser():
"""
Create the argument parser for the main torchserve command.
Lines 17-99.
Returns:
argparse.ArgumentParser: Configured parser with options:
--start: Start the TorchServe server
--stop: Stop the running TorchServe server
--version: Print TorchServe version
--ts-config: Path to TorchServe configuration file
--model-store: Path to model store directory
--models: Models to load at startup (name=url pairs)
--log-config: Path to log4j configuration file
--foreground: Run server in foreground (default: background)
--no-config-snapshots: Disable config snapshot saving
--workflow-store: Path to workflow store directory
--workflows: Workflows to load at startup
--ncs / --no-config-snapshots: Disable config snapshots
--enable-model-api: Enable model management API
"""
...
@staticmethod
def model_service_worker_args():
"""
Create the argument parser for the backend model service worker.
Lines 102-157.
Returns:
argparse.ArgumentParser: Configured parser with options:
--sock-type: Socket type (unix or tcp)
--sock-name: Unix domain socket name
--host: Worker host address
--port: Worker port number
--metrics-config: Path to metrics configuration file
--pid: Worker process ID file path
--model-dir: Path to the model directory
"""
...
@staticmethod
def extract_args(args=None):
"""
Parse and return arguments using ts_parser(). Lines 160-162.
Args:
args (list|None): Argument list to parse. If None, uses sys.argv.
Returns:
argparse.Namespace: Parsed arguments.
"""
return ArgParser.ts_parser().parse_args(args)
Import
from ts.arg_parser import ArgParser
I/O Contract
ts_parser() Arguments
| Argument |
Type |
Default |
Description
|
--start |
flag |
False |
Start the TorchServe server
|
--stop |
flag |
False |
Stop the running TorchServe server
|
--version |
flag |
False |
Print TorchServe version and exit
|
--ts-config |
str |
None |
Path to TorchServe configuration file (config.properties)
|
--model-store |
str |
None |
Path to the model store directory containing .mar files
|
--models |
str (repeatable) |
None |
Models to load at startup in name=url format
|
--log-config |
str |
None |
Path to log4j configuration file
|
--foreground |
flag |
False |
Run server in the foreground (do not daemonize)
|
--no-config-snapshots / --ncs |
flag |
False |
Disable configuration snapshot saving
|
--workflow-store |
str |
None |
Path to the workflow store directory
|
--workflows |
str (repeatable) |
None |
Workflows to load at startup
|
--enable-model-api |
flag |
False |
Enable the model management REST API
|
model_service_worker_args() Arguments
| Argument |
Type |
Default |
Description
|
--sock-type |
str |
None |
Socket type: unix or tcp
|
--sock-name |
str |
None |
Unix domain socket name/path
|
--host |
str |
None |
Worker host address (for TCP sockets)
|
--port |
int |
None |
Worker port number (for TCP sockets)
|
--metrics-config |
str |
None |
Path to metrics configuration file
|
--pid |
str |
None |
Worker process ID file path
|
--model-dir |
str |
None |
Path to the extracted model directory
|
Return Types
| Method |
Returns |
Description
|
ts_parser() |
argparse.ArgumentParser |
Parser for the torchserve CLI
|
model_service_worker_args() |
argparse.ArgumentParser |
Parser for the backend worker CLI
|
extract_args() |
argparse.Namespace |
Parsed argument namespace
|
Usage Examples
Example 1: Starting TorchServe from CLI
# Start TorchServe with a config file and model store
torchserve --start --ts-config config.properties --model-store model_store
# Start with specific models pre-loaded
torchserve --start --model-store model_store --models resnet18=resnet18.mar squeezenet=squeezenet.mar
# Start in foreground mode with model API enabled
torchserve --start --foreground --model-store model_store --enable-model-api
# Stop TorchServe
torchserve --stop
# Check version
torchserve --version
Example 2: Using ArgParser programmatically
from ts.arg_parser import ArgParser
# Parse custom arguments
args = ArgParser.extract_args([
"--start",
"--ts-config", "config.properties",
"--model-store", "/opt/models",
"--foreground",
])
print(f"Start: {args.start}") # True
print(f"Config: {args.ts_config}") # config.properties
print(f"Model store: {args.model_store}") # /opt/models
print(f"Foreground: {args.foreground}") # True
Example 3: Worker argument parsing
from ts.arg_parser import ArgParser
# Parse backend worker arguments
parser = ArgParser.model_service_worker_args()
worker_args = parser.parse_args([
"--sock-type", "tcp",
"--host", "127.0.0.1",
"--port", "9000",
"--metrics-config", "metrics.yaml",
"--model-dir", "/tmp/models/resnet18",
])
print(f"Socket type: {worker_args.sock_type}") # tcp
print(f"Host: {worker_args.host}") # 127.0.0.1
print(f"Port: {worker_args.port}") # 9000
Example 4: Integration with server startup
# Inside the TorchServe startup flow (ts/model_server.py):
from ts.arg_parser import ArgParser
def start():
args = ArgParser.extract_args()
if args.version:
print_version()
return
if args.stop:
stop_server()
return
if args.start:
config = load_config(args.ts_config)
model_store = args.model_store or config.get("model_store")
start_server(
model_store=model_store,
models=args.models,
log_config=args.log_config,
foreground=args.foreground,
)
Related Pages