Programmer’s Guide¶
In This Section¶
Simple Example¶
Here is a very simple example application. It adds two numbers together and writes the result to the console.
1from agiparallel.client import ClusterJobScheduler
2from agiparallel.constants import CommonResources, JobPriority, Operator
3
4
5class SimpleTask():
6 def __init__(self, first, second):
7 self.a = first
8 self.b = second
9
10 def execute(self):
11 self.result = self.a + self.b
12
13
14class MyEnvironment():
15 def setup(self):
16 pass
17
18 def teardown(self):
19 pass
20
21
22def simple_task_example():
23 with ClusterJobScheduler("localhost") as scheduler:
24 scheduler.connect()
25 job = scheduler.create_job()
26
27 # add one or more tasks
28 job.add_task(SimpleTask(1, 1))
29
30 # set the task environment
31 job.task_environment = MyEnvironment()
32
33 # set various job options
34 job.name = "My job name"
35 job.description = "My job description"
36 job.max_interrupted_retry_attempts = 0
37 job.priority = JobPriority.LOW
38 job.add_task_preconditions(CommonResources.AVAILABLE_CORES, Operator.GREATER_THAN, 2)
39
40 # submit task to the job scheduler
41 job.submit()
42
43 # wait for results
44 job.wait_until_done()
45
46 print("The result is: ", job.tasks[0].result)
47
48
49if __name__ == "__main__":
50 simple_task_example()
Copy and paste the code into a python file and run. The application first connects and submits a task to the coordinator. Next, an agent will be assigned the work and execute the task in an isolated process. Finally, the result of the task is sent back to the client.
If you’re using an IDE like JetBrains PyCharm, step through the code line by line. Examine the messages
written from the coordinator and agent consoles. Take note of how the coordinator
and agent respond after stepping through each line. For example, stepping
past job.submit()
will cause the coordinator console to log “added 1 tasks for job #1 to queue”.
What this means will be discussed later.
Continue to experiment with this application.
Try to add more tasks to the job, set the jobs’s task_environment
,
or modify some of the job options. As this example shows, the basic structure and core pieces of an application written with the API are simple but powerful!
Next Steps¶
With a rudimentary example application finished, explore the help system and move on to more advanced topics. The following sections give various topics in the API more treatment.
Title |
Read this if you want to… |
---|---|
Read a step-by-step narrative of another sample application. |
|
Get solutions to common patterns in the API. |
|
Understand the key concepts and building blocks of the product. |
|
Get answers to frequently asked questions. |
|
Gain a firm understanding of the internals of the product. |
|
Explore what is available in the API. |