Added support for specifying the number of worker processes in the CLI and runserver command.

This commit is contained in:
Ricardo Robles 2025-05-10 15:33:30 +02:00
parent 064ca7a567
commit 663cf4a23d
2 changed files with 17 additions and 0 deletions

View File

@ -160,6 +160,13 @@ class CommandLineInterface:
self.parser.add_argument( self.parser.add_argument(
"--no-server-name", dest="server_name", action="store_const", const="" "--no-server-name", dest="server_name", action="store_const", const=""
) )
self.parser.add_argument(
"-w",
"--workers",
type=int,
help="Number of worker processes to spawn",
default=1,
)
self.server = None self.server = None
@ -287,5 +294,6 @@ class CommandLineInterface:
"X-Forwarded-Proto" if args.proxy_headers else None "X-Forwarded-Proto" if args.proxy_headers else None
), ),
server_name=args.server_name, server_name=args.server_name,
workers=args.workers,
) )
self.server.run() self.server.run()

View File

@ -85,6 +85,14 @@ class Command(RunserverCommand):
dest="insecure_serving", dest="insecure_serving",
help="Allows serving static files even if DEBUG is False.", help="Allows serving static files even if DEBUG is False.",
) )
parser.add_argument(
"--workers",
action="store",
dest="workers",
type=int,
default=1,
help="Number of worker processes to spawn (default: 1)",
)
def handle(self, *args, **options): def handle(self, *args, **options):
self.http_timeout = options.get("http_timeout", None) self.http_timeout = options.get("http_timeout", None)
@ -144,6 +152,7 @@ class Command(RunserverCommand):
http_timeout=self.http_timeout, http_timeout=self.http_timeout,
root_path=getattr(settings, "FORCE_SCRIPT_NAME", "") or "", root_path=getattr(settings, "FORCE_SCRIPT_NAME", "") or "",
websocket_handshake_timeout=self.websocket_handshake_timeout, websocket_handshake_timeout=self.websocket_handshake_timeout,
workers=options["workers"],
).run() ).run()
logger.debug("Daphne exited") logger.debug("Daphne exited")
except KeyboardInterrupt: except KeyboardInterrupt: