Skip to content

Commit

Permalink
start dt_worker before download
Browse files Browse the repository at this point in the history
  • Loading branch information
saimanikant committed Nov 29, 2024
1 parent c07e55f commit e7b7b77
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
42 changes: 23 additions & 19 deletions examples/python_large_output/download_output_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@
# SOFTWARE.

"""
Example to query resources from a project.
- Query values from evaluated jobs, computing some simple statistics on parameter values.
- Download files from the project
Example to Download files of evaluated jobs from a project
"""
import argparse
import logging
import os
import tempfile
import time

from ansys.hps.client import Client, HPSError
Expand All @@ -38,37 +35,43 @@
log = logging.getLogger(__name__)


def download_files(client, project_name):
def download_files(client, project_name, dir_path):
"""Download files."""
out_path = os.path.join(os.path.dirname(__file__), "downloads")

out_path = dir_path
if not out_path:
temp_dir = tempfile.TemporaryDirectory()
out_path = os.path.join(temp_dir.name, "downloads")

log.info(f"Downloading files to {out_path}")

jms_api = JmsApi(client)
project = jms_api.get_project_by_name(name=project_name)
project = jms_api.get_project_by_name(name=project_name)
project = jms_api.get_project(id=project.id)

# start dt client to time dowload of output files correctly
jms_api.client._start_dt_worker()

log.info(f"Project id: {project.id}")
project_api = ProjectApi(client, project.id)

jobs = project_api.get_jobs(eval_status="evaluated", fields=["id", "values", "elapsed_time"])
log.info(f"# evaluated jobs: {len(jobs)}")
num = len(jobs)

log.info(
f"=== Example 1: Downloading output files of {num} jobs using ProjectApi.download_file()"
)
for job in jobs[0:num]:
log.info(f"=== Downloading output files of {num} jobs using ProjectApi.download_file()")
for job in jobs:
log.info(f"Job {job.id}")
for task in project_api.get_tasks(job_id=job.id):
log.info(f"Task {task.id}")
files = project_api.get_files(id=task.output_file_ids)
for f in files:
fpath = os.path.join(out_path, f"task_{task.id}")
log.info(f"Download output file {f.evaluation_path} to {fpath}")
start = time.process_time()
project_api.download_file(file=f, target_path=fpath)
log.info(f"Time taken to download output file: {(time.time() - start):.2f} seconds"
)
start = time.time()
project_api.download_file(file=f, target_path=fpath)
log.info(f"Time taken to download output file: {(time.time() - start):.2f} seconds")


if __name__ == "__main__":

Expand All @@ -77,17 +80,18 @@ def download_files(client, project_name):
parser.add_argument("-U", "--url", default="https://127.0.0.1:8443/hps")
parser.add_argument("-u", "--username", default="repuser")
parser.add_argument("-p", "--password", default="repuser")
parser.add_argument("-dir", "--download-path", type=str)
args = parser.parse_args()

logger = logging.getLogger()
logging.basicConfig(format="%(message)s", level=logging.DEBUG)
logging.basicConfig(format="%(message)s", level=logging.INFO)

try:
log.info("Connect to HPC Platform Services")
client = Client(url=args.url, username=args.username, password=args.password)
log.info(f"HPS URL: {client.url}")

download_files(client=client, project_name=args.name)
download_files(client=client, project_name=args.name, dir_path=args.download_path)

except HPSError as e:
log.error(str(e))
log.error(str(e))
7 changes: 6 additions & 1 deletion examples/python_large_output/project_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ def create_project(client, name, use_exec_script, python_version=None) -> Projec
client = Client(url=args.url, username=args.username, password=args.password)

try:
create_project(client, name=args.name, use_exec_script=args.use_exec_script, python_version=args.python_version)
create_project(
client,
name=args.name,
use_exec_script=args.use_exec_script,
python_version=args.python_version,
)
except HPSError as e:
log.error(str(e))

0 comments on commit e7b7b77

Please sign in to comment.