runScript
Method
The runScript
method facilitates executing scripts on Helix Cloud.
Usage
from helixclient import HelixClient
client = HelixClient(api_key='YOUR_API_KEY', api_secret='YOUR_API_SECRET')
# Execute a script from a file
response = client.runScript(file_path='path/to/your/script.py', input={'key': 'value'})
# Execute a script from a string
response = client.runScript(script='print("Hello, world!")', input={'key': 'value'})
# Execute a script with custom environment variables
response = client.runScript(file_path='path/to/your/script.py', input={'key': 'value'}, env={'MY_VAR': 'value'})
Parameters
file_path
: (str, optional) The path to the script file.script
: (str, optional) The script content as a string. Eitherfile_path
orscript
must be provided.input
: (dict, optional) Input data for the script. This data will be accessible within the script as a dictionary namedinput
.env
: (dict, optional) Environment variables to be set for the script execution.
Return Value
The runScript
method returns a dictionary with the following structure:
{
'output': 'Script output',
'error': 'Error message if any',
'exit_code': 0 # Exit code of the script
}
Script Execution Process
- The script is uploaded to Helix Cloud.
- The script is executed within a containerized environment.
- The script’s output, error messages, and exit code are returned.
Error Handling
- If an error occurs during script execution, the
error
field in the response will contain an error message. - The
exit_code
field will indicate the script’s exit code. - You can use the
error
andexit_code
fields to handle script execution errors.
Potential Optimizations
- Cache scripts: Cache frequently used scripts to improve execution speed.
- Parallel execution: Execute multiple scripts concurrently using threading or multiprocessing.
- Optimize input data: Reduce the size of the input data by removing unnecessary elements.
Example
from helixclient import HelixClient
client = HelixClient(api_key='YOUR_API_KEY', api_secret='YOUR_API_SECRET')
# Execute a script from a file with input data
response = client.runScript(file_path='path/to/your/script.py', input={'name': 'John Doe'})
# Print the script output
print(response['output'])