Skip to content

Commit

Permalink
NIFI-13660 ProcessContext.yieldResources() can be called from Python …
Browse files Browse the repository at this point in the history
…code

Closes #9182

Signed-off-by: Marton Szasz <szaszm@apache.org>
  • Loading branch information
pgyori authored and szaszm committed Sep 4, 2024
1 parent 0510e71 commit 5f0cbed
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
6 changes: 4 additions & 2 deletions nifi-docs/src/main/asciidoc/python-developer-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,10 @@ created in the Processor's Python code. `attributes` and `contents` are both opt
the FlowFile will still have the usual `filename`, `path` and `uuid` attributes, but no additional ones.
If `contents` is not provided, a FlowFile with no contents (only attributes) will be created.
In case there is no useful information to return from the `create` method, `return None` can be used instead of returning an
empty `FlowFileSourceResult`. When `create` returns with `None`, the processor does not produce any output.

empty `FlowFileSourceResult`. When `create()` returns with `None`, the processor does not produce any output.
When there is nothing to return, it might be useful to yield the processor's resources and not schedule the processor
to run for the period of time defined by the processor's Yield Duration. This can be achieved by calling
`context.yield_resources()` from the processor's `create` method right before returning `None`.


[[property-descriptors]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ def __init__(self, java_context):
def getName(self):
return self.name

def yield_resources(self):
JvmHolder.java_gateway.get_method(self.java_context, "yield")()


class ValidationContext(PropertyContext):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
import sys
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
from py4j import java_gateway
from py4j.java_gateway import JavaGateway, CallbackServerParameters, GatewayParameters

import ExtensionManager
Expand Down Expand Up @@ -122,6 +123,7 @@ class Java:
from nifiapi.__jvm__ import JvmHolder
JvmHolder.jvm = gateway.jvm
JvmHolder.gateway = gateway
JvmHolder.java_gateway = java_gateway

# We need to import PythonProcessorAdapter but cannot import it at the top of the class because we must first initialize the Gateway,
# since there are statically defined objects in the file that contains PythonProcessorAdapter, and those statically defined objects require the Gateway.
Expand Down

0 comments on commit 5f0cbed

Please sign in to comment.