Using a Python processor
Python processors are loaded from external files, and they keep running a function, while retaining the interpreter state. This makes them ideal for tasks that need to keep state between runs or need some initialization logic to be run only once. Learn how to use custom Python processors to integrate custom code into a MiNiFi C++ agent.
The workflow of a Python processor is as follows:
- At startup, the MiNiFi C++ agent reads the Python script directory specified
minifi.propertiesfile as the value of the
nifi.python.processor.dirproperty. By default, this is MINIFI_HOME/minifi-python.
- The agent scans the directory for compatible scripts and automatically registers them for use.
- The Python files are evaluated at startup. Their
onSchedulefunction is invoked before starting a flow and their
onTriggerfunction is invoked regularly as the processor is scheduled.
For more details, see the following example:
#!/usr/bin/env python def describe(processor): processor.setDescription("Adds an attribute to your flow files") def onInitialize(processor): processor.setSupportsDynamicProperties() def onTrigger(context, session): flow_file = session.get() if flow_file is not None: flow_file.addAttribute("Python attribute", "attributevalue") session.transfer(flow_file, REL_SUCCESS)
To add or update processors, add them to the Python script directory and restart the agent.