Running Python UDFs

The information below includes all information one needs to know to begin running Python UDFs. For more information on writing Python UDFs, see Python UDF API; for more information on simulating running UDFs, see UDF Simulator. Example Python UDFs can be found here.

Important

Though any of the native APIs can be used for running UDFs written in any UDF API language, all the examples below are written using the native Python API.

Deployment

Calling the create_proc() method will deploy the specified UDF to the Kinetica execution environment, to every server in the cluster. The method takes the following parameters:

ParameterDescription
proc_nameA system-wide unique name for the UDF
execution_modeAn execution mode; either distributed or nondistributed
files

A set of files composing the UDF package, including the names of the files and the binary data for those files--the files specified will be created on the target Kinetica servers (default is the /opt/gpudb/procs/ directory), with the given data and filenames; if the filenames contain subdirectories, that structure will be copied to the target servers

Note: Uploading files using the files parameter should be reserved for smaller files; larger files should be uploaded to KiFS. See the Python UDF Tutorial for an example.

commandThe name of the command to run, which can be a file within the deployed UDF fileset, or any command able to be executed within the host environment, e.g., /opt/gpudb/bin/gpudb_python or python. If a host environment command is specified, the host environment must be properly configured to support that command's execution.
argsA list of command-line arguments to pass to the specified command, e.g., ./<file name>.py
optionsOptional parameters for UDF creation; see create_proc() for details

For example, to deploy a Python UDF using the native Python API, a local proc file (udf_tc_py_proc.py) and CSV file (rank_tom.csv) will need to be read in as bytes and then passed into the create_proc() call as values paired with their keys inside map files.

1
2
3
OUTPUT_TABLE = SCHEMA + '.udf_tc_py_out_table'

proc_name = 'udf_tc_py_proc'
1
2
3
4
5
6
    file_names = (csv_file_name, proc_file_name)
        with open(file_name, 'rb') as file:
            files[file_name] = file.read()

    # Remove proc if it exists from a prior registration
    if kinetica.has_proc(proc_name=proc_name)["proc_exists"]:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
    )
    print("Proc created successfully:")
    print(response)
    print("")

    print("Executing proc...")
    response = kinetica.execute_proc(
        proc_name=proc_name,
        params={},
        bin_params={},
        input_table_names=[INPUT_TABLE],

Concurrency Limits

The max_concurrency_per_node setting is available in the options map of the /create/proc. This option allows you to define a per-Kinetica- host concurrency limit for a UDF, i.e. no more than n OS processes (UDF instances) in charge of evaluating the UDF will be permitted to execute concurrently on a single Kinetica host. You may want to set a concurrency limit if you have limited resources (like GPUs) and want to avoid the risks of continually exhausting your resources. This setting is particularly useful for distributed UDFs, but it will also work for non-distributed UDFs.

Note

You can also set concurrency limits on the Edit Proc screen in the UDF section of GAdmin

The default value for the setting is 0, which results in no limits. If you set the value to 4, only 4 instances of the UDF will be queued to execute the UDF. This holds true across all invocations of the proc; this means that even if /execute/proc is called eight times, only 4 processes will be running. Another instance will be queued as soon as one instance finishes processing. This process will repeat, only allowing 4 instances of the UDF to run at a time, until all instances have completed or the UDF is killed.

Execution

Calling the execute_proc() method will execute the specified UDF within the targeted Kinetica execution environment. The method takes the following parameters:

ParameterDescription
proc_nameThe system-wide unique name for the UDF
paramsSet of string-to-string key/value paired parameters to pass to the UDF
bin_paramsSet of string-to-binary key/value paired parameters to pass to the UDF
input_table_namesInput data table names, to be processed by the UDF
input_column_namesMapping of input data table names to their respective column names, to be processed as input data by the UDF
output_table_namesOutput data table names, where processed data is to be appended
optionsOptional parameters for UDF execution; see execute_proc() for details

The call is asynchronous and will return immediately with a run_id, which is a string that can be used in subsequent checks of the execution status.

For example, to execute a proc that's already been created (udf_tc_py_proc) using existing input (udf_tc_py_in_table) and output (udf_tc_py_out_table) tables:

1
2
SCHEMA = 'tutorial_udf_python'
OUTPUT_TABLE = SCHEMA + '.udf_tc_py_out_table'
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
        options={}
    )
    print("Proc executed successfully:")
    print(response)
    print("Check the system log or 'gpudb-proc.log' for execution information")
    print("")

# end python_tc_udf_exec()

if __name__ == '__main__':

    # Set up args
    parser = argparse.ArgumentParser(description='Execute the table copy Python UDF example.')

Management

UDFs can be managed using either GAdmin or one of the methods below:

  • show_proc_status() -- Returns whether the UDF (or UDFs) is still running, has completed, or has exited with an error, along with any processed results
  • show_proc() -- returns the parameter values used in creating the UDF
  • has_proc() -- returns whether the given UDF exists
  • kill_proc() -- terminates a running UDF (or UDFs)
  • delete_proc() -- removes the given UDF definition from the system; needs to be called before create_proc() when recreating a UDF