Python LSAPI Configuration

Using Apache Settings

With the Python module, LSWS will automatically detect and use Apache mod_wsgi settings. The Python LSAPI module is compatible with the following mod_wsgi directives:

WSGIApplicationGroup
WSGICallableObject
WSGIScriptReloading
WSGIScriptAlias
WSGIScriptAliasMatch

Using LiteSpeed Native Settings

External Application Settings

LSAPI external application settings are explained in the LiteSpeed Web Server documentation. You can achieve the results of the Apache directives listed above by using script handlers and contexts in LSWS. These settings are also explained in the LSWS documentation.

Environment Variables

Python LSAPI applications can be further configured using the environment variables listed below:

LSAPI_CHILDREN (default: 0)

LSAPI allows a variety of setups. Two of these setups, Worker and ProcessGroup can be set using this environment variable. (Note: In previous versions of this documentation, Worker was referred to as "server managed mode" and ProcessGroup referred to as "self managed mode".)

Setting LSAPI_CHILDREN to <=1 puts LSWS in Worker mode. In Worker mode, LiteSpeed Web Server dynamically spawns new Python processes to meet demand and kills finished processes. In this mode, an external application's Instances setting (WebAdmin console > Configuration > External App > your external application) should match the Max Connections setting (WebAdmin console > Configuration > External App > your external application).

Setting LSAPI_CHILDREN to >1 puts LSWS in ProcessGroup mode. In ProcessGroup mode, the web server will start one constantly-running Python parent process, and this process will fork child Python processes (as opposed to spawning new processes) to meet demand. ProcessGroup is generally preferred because all Python processes can then share one memory block for opcode caching. In ProcessGroup mode, Instances should be set to 1, while LSAPI_CHILDREN should be set to match the value of Max Connections. Usually, there is no need to set LSAPI_CHILDREN over 100.

LSAPI_AVOID_FORK
(default: 0)

LSAPI_AVOID_FORK specifies whether the internal process manager in ProcessGroup mode should try to avoid forking new child processes.

When set to 0, the internal process manager will not try to avoid forking new processes. To save system resources, it will stop processes when they finish and only start child processes when they are needed. This is often preferred in shared hosting.

When set to 1, the internal process manager will try to avoid frequently stopping and starting child processes. This might be preferred in a dedicated hosting environment because it may be faster to recycle existing processes, even if it means running processes when they are unused sometimes.

LSAPI_EXTRA_CHILDREN (default: 1/3 of LSAPI_CHILDREN or 0)

In ProcessGroup mode, LSAPI_EXTRA_CHILDREN controls the maximum number of extra child processes that can be started when existing child processes are malfunctioning. The total number of child processes will be reduced to the level set in LSAPI_CHILDREN as soon as service is back to normal. When LSAPI_AVOID_FORK is set to 0, the default value of LSAPI_EXTRA_CHILDREN is 1/3 of LSAPI_CHIDLREN. When LSAPI_AVOID_FORK is set to 1, the default value is 0.

LSAPI_MAX_REQS (default: 10000)

In ProcessGroup mode, this controls how many requests each child process will handle before it exits automatically. This parameter can help reduce memory usage by leaky Python functions.

LSAPI_MAX_IDLE (default: 300 seconds)

In ProcessGroup mode, LSAPI_MAX_IDLE controls how long an idle child process will wait for a new request before it exits. This option helps release system resources taken by idle processes.

LSAPI_MAX_IDLE_CHILDREN (default: 1/3 of LSAPI_CHILDREN or LSAPI_CHILDREN)

In ProcessGroup mode, LSAPI_MAX_IDLE_CHILDREN controls how many idle child processes are allowed. Extra idle child processes will be killed by the parent process immediately. When LSAPI_AVOID_FORK is set to 0, the default value of LSAPI_MAX_IDLE_CHILDREN is 1/3 of LSAPI_CHIDLREN. When LSAPI_AVOID_FORK is set to 1, the default value of LSAPI_MAX_IDLE_CHILDREN is the same as the value of LSAPI_CHILDREN.

LSAPI_MAX_PROCESS_TIME (default: 300 seconds)

In ProcessGroup mode, LSAPI_MAX_PROCESS_TIME controls the maximum processing time allowed when processing a request. If a child process can not finish processing a request in the given time period, it will be killed by the parent process. This option can help get rid of dead or runaway child processes.

LSAPI_PGRP_MAX_IDLE (default: FOREVER )

In ProcessGroup mode, LSAPI_PGRP_MAX_IDLE controls how long the parent process will wait before exiting when there are no child processes. This option can help release system resources taken up by an idle parent process. This environment variable has the same function as the Max Idle Time setting (WebAdmin console > Configuration > External App).

LSAPI_PPID_NO_CHECK

By default, an LSAPI external application will exit automatically if the parent process dies. This is to reduce orphan processes when the web server is restarted. However, it may be desirable to disable this feature in situations such as when an LSAPI process was started manually from the command line. Adding the LSAPI_PPID_NO_CHECK environment variable (set it to 1) will disable the checking for the existence of a parent process. To turn off this setting, remove the environment variable completely.

LSAPI_ALLOW_CORE_DUMP

By default, an LSAPI application will not leave a core dump file when it crashes. If you want to have LSAPI dump a core file, you should add this environment variable and set to 1. If set, core files will be created under the current working directory, generally the directory of the Python script that crashed. To turn off this setting, remove the environment variable completely.

STAY CONNECTED