How to pass argument to worker

I would like to be able to pass a set of arguments from the web app to the worker and on to to the command.bat and the python app that runs to set options. I see in config.yaml that you can specify arguments but not sure how to set them from options and pull down menus from the parametrization in the viktor app. Code snippets would be much appreciated. Thank you!


The config files will do command line input on your machine. When you do this manually, the input looks as follows:

C:\Users\yourname> ToolLauncher.exe inputfile --setting1 --setting2 

this has three components:

  • the executable name or path, in this case “ToolLauncher.exe”
  • the arguments to the executable, in this case there are three: inputfile, --setting1 and --setting2
  • and finally the working directory, in this case: C:\Users\yourname

This will correspond to the following config file:

    path: 'ToolLauncher.exe'
    - 'inputfile' 
    - '--setting1' 
    - '--setting2'
    workingDirectoryPath: 'C:\Users\yourname'

You can leave the workingDirectoryPath empty if it does not matter from which folder the command is run, and you can also leave the arguments empty. The executable key ToolLauncher can be any name, to be used in your app code to know which excecutable should be started in case you have multiple.

In your app code you will have the following:

from viktor.external.generic import GenericAnalysis

# Generate the input file(s)
files = [
    ('input1.txt', file1),
    ('input2.txt', file2)

# Run the analysis and obtain the output file.
generic_analysis = GenericAnalysis(files=files, executable_key="ToolLauncher", output_filenames=["output.txt"])
output_file = generic_analysis.get_output_file("output.txt")

This tells the worker to:

  • Create two files to the machine called
    • input1.txt containing the string or bytes file1
    • input2.txt containing file2.
    • In case you specified a WorkingDirectoryPath, they will appear there (in this case the folder C:\Users\yourname) otherwise it will be a temporary folder somewhere on the machine
  • Run the command ToolLauncher.exe inputfile --setting1 --setting2
  • Wait until the expected output file appears, called output.txt, at most 60 seconds and send it back

I hope this makes it more clear,
kind regards

Yes, thank you. I already have all that working, but my question is: Is there any way to pass --setting1 and --setting2 programmatically from the viktor web app? In my case, they are not ‘constant’ I need to send them from the web app.

No, unfortunately not. Sometimes I make the .bat file in my app code and send that to the worker as a full file. that is done in this example

May I ask what is the exact case? I have never needed that previously, so it would be interesting for us to know why you need variable arguments.

ok thanks. Basically, I am running energy analysis on a remote server and I need to tell it what results to send back. This takes the form of:
reports (e.g. “HVACSizingSummary”, “HVACSizingSummary”, “SystemSummary”)
tableNames (e.g. “Zone Sensible Cooling”, “Zone Sensible Heating”, “Time Not Comfortable Based on Simple ASHRAE 55-2004”)

columnNames (e.g. “User Design Load”, “User Design Load”, “Summer or Winter Clothes”)

unitsList = (e.g. “W” or “hr”)

As you said, a workaround would be to include all these in a structured file and have my script read it. No worries. Can be done.

If the number of possibilities is low and sending back all results is problematic, you could also consider specifying multiple keys under executables I believe?

    path: 'ToolLauncher.exe'
    workingDirectoryPath: 'C:\Users\yourname'
    path: 'ToolLauncher.exe'
    - '--x'
    workingDirectoryPath: 'C:\Users\yourname'
    path: 'ToolLauncher.exe'
    - '--y'
    workingDirectoryPath: 'C:\Users\yourname'
    path: 'ToolLauncher.exe'
    - '--x'
    - '--y'
    workingDirectoryPath: 'C:\Users\yourname'

1 Like