| 1 |
app_python3s Module |
| 2 |
|
| 3 |
Daniel-Constantin Mierla |
| 4 |
|
| 5 |
Maxim Sobolev |
| 6 |
|
| 7 |
Anthony Alba |
| 8 |
|
| 9 |
Edited by |
| 10 |
|
| 11 |
Daniel-Constantin Mierla |
| 12 |
|
| 13 |
Copyright © 2022 kamailio.org |
| 14 |
__________________________________________________________________ |
| 15 |
|
| 16 |
Table of Contents |
| 17 |
|
| 18 |
1. Admin Guide |
| 19 |
|
| 20 |
1. Overview |
| 21 |
2. Dependencies |
| 22 |
|
| 23 |
2.1. Kamailio Modules |
| 24 |
2.2. External Libraries or Applications |
| 25 |
|
| 26 |
3. Parameters |
| 27 |
|
| 28 |
3.1. load (string) |
| 29 |
3.2. script_init (string) |
| 30 |
3.3. script_child_init (string) |
| 31 |
3.4. threads_mode (int) |
| 32 |
|
| 33 |
4. Functions |
| 34 |
|
| 35 |
4.1. app_python3s_exec(method [, param]) |
| 36 |
|
| 37 |
5. RPC Commands |
| 38 |
|
| 39 |
5.1. app_python.reload |
| 40 |
5.2. app_python.api_list |
| 41 |
|
| 42 |
6. KEMI Usage |
| 43 |
|
| 44 |
List of Examples |
| 45 |
|
| 46 |
1.1. Set load parameter |
| 47 |
1.2. Set script_init parameter |
| 48 |
1.3. Set script_child_init parameter |
| 49 |
1.4. Set threads_mode parameter |
| 50 |
1.5. app_python3s_exec usage |
| 51 |
|
| 52 |
Chapter 1. Admin Guide |
| 53 |
|
| 54 |
Table of Contents |
| 55 |
|
| 56 |
1. Overview |
| 57 |
2. Dependencies |
| 58 |
|
| 59 |
2.1. Kamailio Modules |
| 60 |
2.2. External Libraries or Applications |
| 61 |
|
| 62 |
3. Parameters |
| 63 |
|
| 64 |
3.1. load (string) |
| 65 |
3.2. script_init (string) |
| 66 |
3.3. script_child_init (string) |
| 67 |
3.4. threads_mode (int) |
| 68 |
|
| 69 |
4. Functions |
| 70 |
|
| 71 |
4.1. app_python3s_exec(method [, param]) |
| 72 |
|
| 73 |
5. RPC Commands |
| 74 |
|
| 75 |
5.1. app_python.reload |
| 76 |
5.2. app_python.api_list |
| 77 |
|
| 78 |
6. KEMI Usage |
| 79 |
|
| 80 |
1. Overview |
| 81 |
|
| 82 |
This module is an alternative 'app_python3' module with static export |
| 83 |
of 'KSR' object and functions, without instantiating the SIP message |
| 84 |
object. |
| 85 |
|
| 86 |
This module cannot be loaded together with 'app_python3' as some global |
| 87 |
symbols conflict. |
| 88 |
|
| 89 |
This module allows executing Python3 scripts from the config file, |
| 90 |
exporting functions to access the SIP message from Python3. |
| 91 |
|
| 92 |
Note: if symbols exported to KEMI (module or function names) conflict |
| 93 |
with Python's reserved keywords, use the 'getattr()' function or the |
| 94 |
'__dict__' attribute for 'KSR' (e.g., |
| 95 |
'KSR.__dict__["async"].task_route("myroute")'). |
| 96 |
|
| 97 |
2. Dependencies |
| 98 |
|
| 99 |
2.1. Kamailio Modules |
| 100 |
2.2. External Libraries or Applications |
| 101 |
|
| 102 |
2.1. Kamailio Modules |
| 103 |
|
| 104 |
The following modules must be loaded before this module: |
| 105 |
* none. |
| 106 |
|
| 107 |
2.2. External Libraries or Applications |
| 108 |
|
| 109 |
The following libraries or applications must be installed before |
| 110 |
running Kamailio with this module loaded: |
| 111 |
* python3 - Python 3 runtime. |
| 112 |
|
| 113 |
To compile this module the Python 3 development package is needed. |
| 114 |
Requirements: |
| 115 |
* python3-dev - Python 3 development package. |
| 116 |
* python3-config - (part of python3-dev) tool to output C includes |
| 117 |
and library paths. |
| 118 |
|
| 119 |
3. Parameters |
| 120 |
|
| 121 |
3.1. load (string) |
| 122 |
3.2. script_init (string) |
| 123 |
3.3. script_child_init (string) |
| 124 |
3.4. threads_mode (int) |
| 125 |
|
| 126 |
3.1. load (string) |
| 127 |
|
| 128 |
The path to the file with Python code to be executed from configuration |
| 129 |
file. |
| 130 |
|
| 131 |
Default value is “/usr/local/etc/kamailio/kamailio.py”. |
| 132 |
|
| 133 |
Example 1.1. Set load parameter |
| 134 |
... |
| 135 |
modparam("app_python3s", "load", "/usr/local/etc/kamailio/myscript.py") |
| 136 |
... |
| 137 |
|
| 138 |
3.2. script_init (string) |
| 139 |
|
| 140 |
The name of the Python function to be executed when the script is |
| 141 |
loaded or reloaded. |
| 142 |
|
| 143 |
Default value is “” (not set). |
| 144 |
|
| 145 |
Example 1.2. Set script_init parameter |
| 146 |
... |
| 147 |
modparam("app_python3s", "script_init", "ksr_script_init") |
| 148 |
... |
| 149 |
def ksr_script_init(): |
| 150 |
KSR.info("init python script\n") |
| 151 |
return 1 |
| 152 |
... |
| 153 |
|
| 154 |
3.3. script_child_init (string) |
| 155 |
|
| 156 |
The name of the Python function to be executed when Kamailio forks |
| 157 |
child processes at startup and when the script is reloaded. |
| 158 |
|
| 159 |
Default value is “” (not set). |
| 160 |
|
| 161 |
Example 1.3. Set script_child_init parameter |
| 162 |
... |
| 163 |
modparam("app_python3s", "script_child_init", "ksr_script_child_init") |
| 164 |
... |
| 165 |
def ksr_script_child_init(): |
| 166 |
KSR.info("child init python script\n") |
| 167 |
return 1 |
| 168 |
... |
| 169 |
|
| 170 |
3.4. threads_mode (int) |
| 171 |
|
| 172 |
Control how locking for Python interpreter threads in done. If set to |
| 173 |
1, use Py_BLOCK_THREADS/Py_UNBLOCK_THREADS (new mode added for v6.0.x). |
| 174 |
If set to 0, use PyGILState_Ensure()/PyGILState_Release() (the mode |
| 175 |
implemented initially). |
| 176 |
|
| 177 |
If set to 1, Python threading Thread objects created in the KEMI script |
| 178 |
will be scheduled. Otherwise, additional Python threads will not be |
| 179 |
scheduled (no release of GIL). |
| 180 |
|
| 181 |
Default value is “0”. |
| 182 |
|
| 183 |
Example 1.4. Set threads_mode parameter |
| 184 |
... |
| 185 |
modparam("app_python3s", "threads_mode", 1) |
| 186 |
... |
| 187 |
|
| 188 |
4. Functions |
| 189 |
|
| 190 |
4.1. app_python3s_exec(method [, param]) |
| 191 |
|
| 192 |
4.1. app_python3s_exec(method [, param]) |
| 193 |
|
| 194 |
Execute the Python function with the name given by the parameter |
| 195 |
'method'. Optionally can be provided a second string with the parameter |
| 196 |
to be passed to the Python function. |
| 197 |
|
| 198 |
Both parameters can contain pseudo-variables. |
| 199 |
|
| 200 |
Example 1.5. app_python3s_exec usage |
| 201 |
... |
| 202 |
app_python3s_exec("my_python_function"); |
| 203 |
app_python3s_exec("my_python_function", "my_params"); |
| 204 |
app_python3s_exec("my_python_function", "$rU"); |
| 205 |
... |
| 206 |
|
| 207 |
5. RPC Commands |
| 208 |
|
| 209 |
5.1. app_python.reload |
| 210 |
5.2. app_python.api_list |
| 211 |
|
| 212 |
5.1. app_python.reload |
| 213 |
|
| 214 |
IMPORTANT: this is not thread-safe. In your Python script do not use C |
| 215 |
extensions with threads that call into apy_exec(). |
| 216 |
|
| 217 |
Marks the need to reload the Python script. The actual reload is done |
| 218 |
in each worker when it next invokes a Python method. The module uses a |
| 219 |
worker process lock to prevent recursive reloads. |
| 220 |
|
| 221 |
This function only reloads (re-executes) the user script and creates a |
| 222 |
new script object. It does not reinitialize the interpreter (references |
| 223 |
in the old module remain if not redefined by the new version). |
| 224 |
|
| 225 |
Name: app_python.reload |
| 226 |
|
| 227 |
Parameters: none |
| 228 |
|
| 229 |
Example: |
| 230 |
... |
| 231 |
kamctl rpc app_python3s.reload |
| 232 |
... |
| 233 |
|
| 234 |
Note that reload is done for the Python script provided as parameter to |
| 235 |
this Kamailio module. To reload the Python libraries imported in this |
| 236 |
script, leverage "script_init" and use something like: |
| 237 |
... |
| 238 |
import mod1 |
| 239 |
... |
| 240 |
import modN |
| 241 |
from importlib import reload |
| 242 |
|
| 243 |
def ksr_script_init(): |
| 244 |
reload(mod1) |
| 245 |
... |
| 246 |
reload(modN) |
| 247 |
return kamailio() |
| 248 |
... |
| 249 |
|
| 250 |
Where "modX" are the modules imported at the top. |
| 251 |
|
| 252 |
5.2. app_python.api_list |
| 253 |
|
| 254 |
List the functions available via Kemi framework. |
| 255 |
|
| 256 |
Name: app_python.api_list |
| 257 |
|
| 258 |
Parameters: none |
| 259 |
|
| 260 |
Example: |
| 261 |
... |
| 262 |
kamctl rpc app_python3s.api_list |
| 263 |
... |
| 264 |
|
| 265 |
6. KEMI Usage |
| 266 |
|
| 267 |
The module exports KEMI engine with id "python". |
| 268 |
|
| 269 |
Example: |
| 270 |
... |
| 271 |
loadmodule "app_python3s.so" |
| 272 |
... |
| 273 |
cfgengine "python" |
| 274 |
... |
| 275 |
|
| 276 |
For more details about KEMI, see: |
| 277 |
https://www.kamailio.org/docs/tutorials/devel/kamailio-kemi-framework/ |