Set Host Working Directory

Problem

You want to set the working directory of the host.

Solution

To set the host’s working directory, set working_directory on the job’s task environment and pass the path of the desired working directory. The string will be resolved by expanding the string as an environment variable. For instance, “%TEMP%” will resolve to the full temporary directory path. The task will fail if the directory does not exist on the agent machine.

 1from agiparallel.client import *
 2from agiparallel.infrastructure import *
 3
 4
 5class ReturnsWorkingDirectory(object):
 6    def execute(self):
 7        from os import getcwd
 8        self.result = getcwd()
 9
10
11if __name__ == "__main__":
12    with ClusterJobScheduler("localhost") as client:
13        client.connect()
14        job = client.create_job()
15        job.add_task(ReturnsWorkingDirectory())
16        job.task_environment.working_directory = "%TEMP%"
17        job.submit()
18        job.wait_until_done()
19        print(f"Working directory: {job.tasks[0].result}")

Discussion

By default the working directory of the host process is the agent bin folder. There are some cases where a task needs a user defined working directory. The most common of these cases is that the application assumes a relative path to data. Sometimes this can be avoided by modifying the application itself. However, setting the working directory is an option in cases where modifying the application is not possible.