Chapter 12. Running a Command-Line Program

You can run any command-line program from a Gamma script. Here is an example for a Python program.

[Note]

Another option for Python users is the DataHub API for Python. This will give you more flexibility in handling the results of a computation. It can be integrated with a DataHub instance using a Gamma script that starts the Python program and then lets it run continuously in the background. Alternatively, using Gamma with a Python script as shown below offers more fine-grained access to the data points, with lower latency.

Code

/* 
 * Example script to run a Python script and to collect the result
 */

require ("Application");
require ("AsyncRun");

class RunPython Application
{
    python = "python";
    command = "-c \"print(2 + 3)\"";
}

/*
 * Execute python
 *  self in the callback argument is not evaluated with "@", so it 
 *  refers to  the AsyncCommand instance, not this script instance.
 *  The AsyncCommand class is defined in 
 *  C:\Program Files\Cogent\Cogent DataHub\require\AsyncRun.g
 */
method RunPython.run ()
{
    RunCommandAsync(nil, nil, string(.python, " ", .command), 
    _config_path_, `(@self).processResult(self));
}

/*
 * Process the python result.
 */
method RunPython.processResult(command)
{
    princ ("Python command: ", command.cmdline, "\n");
    princ ("   Result code: ", command.exitcode, "\n");
    princ (" Result string: ", command.resultstring, "\n");
}

/* Write the 'main line' of the program here. */
method RunPython.constructor ()
{
    .run();
}

/* Start the program by instantiating the class. */
ApplicationSingleton (RunPython);