Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions examples/client/python/simple_client_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# Licensed under the Apache License, Version 2.0 https://aws.amazon.com/apache-2-0/

from api.connector import HTCGridConnector
from api.session import GridSession

import time
import os
import json
import logging

try:
client_config_file = os.environ['AGENT_CONFIG_FILE']
except:
client_config_file = "/etc/agent/Agent_config.tfvars.json"

with open(client_config_file, 'r') as file:
client_config_file = json.loads(file.read())


TOTAL_COUNT = 0
# Sample function callback
def sample_callback(worker_lambda_response):
global TOTAL_COUNT
TOTAL_COUNT += 1
print(f"{TOTAL_COUNT}\tOK: {worker_lambda_response}")

# do some computation

pass

if __name__ == "__main__":

logging.info("Simple Client V2")
try:
username = os.environ['USERNAME']
except KeyError:
username = ""
try:
password = os.environ['PASSWORD']
except KeyError:
password = ""


# <1.> Establishes connection to one of many available HTC-Grids
grid_connector = HTCGridConnector(client_config_file, username=username, password=password)

# <2.> Authentication based on the configuration file above
grid_connector.authenticate()


# <3.> Create session object with corresponding context & callback
context = {
"session_priority" : 1
}

grid_session = grid_connector.create_session(
service_name="MyService1",
context=context,
callback=sample_callback)


grid_session_2 = grid_connector.create_session(
service_name="MyService1",
context=context,
callback=sample_callback)


# <4.> Submit tasks for the session
task_1_definition = {
"worker_arguments": ["1000", "1", "1"]
}

task_2_definition = {
"worker_arguments": ["2000", "1", "1"]
}

grid_session.send([task_1_definition, task_2_definition])

grid_session_2.send([task_1_definition, task_2_definition])

# <5.> Submit additional tasks within the same session
time.sleep(1)
grid_session.send([task_1_definition, task_2_definition])

grid_session_2.send([task_1_definition, task_2_definition])


# Blocking wait for completion
grid_session.wait_for_completion(timeout_ms=3000)

print(grid_session.submitted_task_ids)

print(grid_session.received_task_ids)

grid_session.wait_for_completion()
grid_session_2.wait_for_completion()

# grid_session.cancel()

# Close session
grid_session.close()
grid_session_2.close()

# Close connector and stop thread
grid_connector.close(wait_for_sessions_completion=True)

1 change: 1 addition & 0 deletions examples/submissions/k8s_jobs/Dockerfile.Submitter
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ RUN pip install -r requirements.txt

COPY ./examples/client/python/client.py .
COPY ./examples/client/python/simple_client.py .
COPY ./examples/client/python/simple_client_v2.py .
COPY ./examples/client/python/portfolio_pricing_client.py .
COPY ./examples/client/python/sample_portfolio.json .

Expand Down
2 changes: 2 additions & 0 deletions examples/submissions/k8s_jobs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ generated:

mkdir -p $(GENERATED) && cat portfolio-pricing-book.yaml.tpl | sed "s/{{account_id}}/$(ACCOUNT_ID)/;s/{{region}}/$(REGION)/;s/{{image_name}}/$(SUBMITTER_IMAGE_NAME)/;s/{{image_tag}}/$(TAG)/" > $(GENERATED)/portfolio-pricing-book.yaml

mkdir -p $(GENERATED) && cat simple-task-v2-test.yaml.tpl | sed "s/{{account_id}}/$(ACCOUNT_ID)/;s/{{region}}/$(REGION)/;s/{{image_name}}/$(SUBMITTER_IMAGE_NAME)/;s/{{image_tag}}/$(TAG)/" > $(GENERATED)/simple-task-v2-test.yaml

clean:
rm -rf $(GENERATED)
40 changes: 40 additions & 0 deletions examples/submissions/k8s_jobs/simple-task-v2-test.yaml.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
apiVersion: batch/v1
kind: Job
metadata:
name: simple-client-v2-test
spec:
template:
spec:
containers:
- name: generator
securityContext:
{}
image: {{account_id}}.dkr.ecr.{{region}}.amazonaws.com/{{image_name}}:{{image_tag}}
imagePullPolicy: Always
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 100m
memory: 128Mi
command: ["python3","./simple_client_v2.py"]
volumeMounts:
- name: agent-config-volume
mountPath: /etc/agent
env:
- name: INTRA_VPC
value: "1"
restartPolicy: Never
nodeSelector:
grid/type: Operator
tolerations:
- effect: NoSchedule
key: grid/type
operator: Equal
value: Operator
volumes:
- name: agent-config-volume
configMap:
name: agent-configmap
backoffLimit: 0
Loading