PyFunc

Inheritance diagram of PyFunc

PyFunc inheritance diagram

class savant.base.pyfunc.PyFunc(module, class_name, kwargs=None, dev_mode=False)

PyFunc structure that defines instantiation parameters for an object implementing BasePyFuncImpl.

To instantiate the specified module and class after configuration was finished, call load_user_code().

The instance will be available through instance property.

A pyfunc can be configured to be run in dev mode. Pyfuncs in dev mode are reloaded at runtime before each call (each frame) if the source file has changed since the last one. Additionally, pyfuncs in dev mode gracefully recover from error in code, allowing the pipeline to stay up and continue processing.

Note

This class is meant to be defined in a module config, not instantiated directly.

Note

Check pipeline element hierarchy to see which pipeline elements use PyFunc structure.

For example, define a PyFuncElement

- element: pyfunc
  module: module.pyfunc_implementation_module
  class_name: PyFuncImplementationClass

where PyFuncImplementationClass inherits from BasePyFuncPlugin or from NvDsPyFuncPlugin for Deepstream pipeline.

module: str

Name to import or module path.

class_name: str

Python class name to instantiate.

kwargs: Dict[str, Any] | None = None

Class initialization keyword arguments.

dev_mode: bool = False

Whether the pyfunc is in dev mode.

load_user_code()

Load (or reload) the user module/class specified in the PyFunc fields. It’s necessary to call this at least once before starting the pipeline.

property instance: BasePyFuncImpl

Returns resolved PyFunc implementation.

__call__(*args, **kwargs)

Calls resolved PyFunc implementation if its a subclass of BasePyFuncCallableImpl, otherwise no-op.

check_reload()

Checks if reload is needed and reloads in case it is.