The run module is arguably the most important part of the alma interface. It is here where the actual experiment/task has to be implemented. For pyalma the run module is merely a python file implementing a specified interface so that pyalma can load and execute it. Let's have a look at a short but yet extensive example.
import random
import os
def run(instance, save, state):
lines = open(instance).readlines()
num_line_to_extract = int(state) if state != "" else 10
with open("lines.txt", "a") as data_file:
while num_line_to_extract > 0:
data_file.write(random.choice(lines))
num_line_to_extract -= 1
save(str(num_line_to_extract))
# optional
def done():
os.system("systemctl suspend")
This example implementation extracts 10 lines of each instance and accumulates them in a file called lines.txt
. After every line we tell pyalma to save the number of lines left. This happens in the save(str(num_line_to_extract))
line, where we call the state saving callback (save(data : string)
) provided by the framework. When run(...)
is being called it might not be the first time. Maybe it actually got called on that instance earlier on but got interrupted before all 10 lines where extracted. Luckily we saved our state
(number of lines left) and pyalma is kind enough to pass us the saved state. With some sanity checking (if we never saved any state pyalma will pass us an empty string) we can load the number of lines left and we are able to continue our work where got interrupted.
After everything is done, meaning run(...)
got executed on all instances, pyalma will call the done()
function if specified. We decided to go to lunch while our experiment runs and to save electricity we implemented a done()
function to suspend the computer after he is done extracting random lines.
As the name suggest the run(...)
function is what the pyalma worker will run on all specified instances. It takes three parameters:
instance : string
save : void(string)
state : string
Implementing this function is optional but can be useful if some actions have to be executed after everything is done. If specified/implemented pyalma will call done()
after run(...)
got called on all instances and terminated successfully for every instance. Interrupted executions will be called again.