SpeedUpPythonPlugins

From NagiosCommunity

Jump to: navigation, search

How to speed up Pyhton plugins

Why?

More and more plugins are written in Python. Since Python is an interpreted programming language and Nagios does not have any sort of embedded python every execution of a plugin causes a translation of Python into bytecode. The translation of plugins with a lot of imports can cost additional cpu seconds.

How?

Before starting to translate a script the Python interpreter automaticly looks inside the same directory for the bytecode of it. The extension of Python bytecode is .pyc instead of .py for not translated scripts. If it exists and isn't older than the script Python loads the bytecode directly . Such a bytecode doesn't run any faster than .py files but the loading is faster.

So before using a Python plugin first run it as user with write access to the directories where the Python script and its modules are. Than check if all modules (look for "import ..." inside the script) are compiled into bytecode. Please, note that the main script executed by Python, even if its filename ends in .py, is not compiled to a .pyc file. It is compiled to bytecode, but the bytecode is not saved to a file. Usually main scripts are quite short, so this doesn't cost much speed. If you want to force Python to translate the main script also into bytecode use this way:

 python -c "import [name_of_the_plugin_without_.py]"

Than the translated version of your script will be stored inside the same directory.

Personal tools
news feeds