Project

General

Profile

Actions

Python SDK » History » Revision 4

« Previous | Revision 4/17 (diff) | Next »
Brett Smith, 08/28/2014 09:29 AM


Python SDK

(design draft)

Example crunch script

#!/usr/bin/env python

from arvados import CrunchJob

import examplelib
import re

class NormalizeMatchingFiles(CrunchJob):
    @CrunchJob.task()
    def grep_files(self):
        # CrunchJob instantiates input parameters based on the
        # dataclass attribute.  When we ask for the input parameter,
        # CrunchJob sees that it's a Collection, and returns a
        # CollectionReader object.
        input_coll = self.job_param('input')
        for filename in input_coll.filenames():
            self.grep_file(self.job_param('pattern'), input_coll, filename)

    @CrunchJob.task()
    def grep_file(self, pattern, collection, filename):
        regexp = re.compile(pattern)
        with collection.open(filename) as in_file:
            for line in in_file:
                if regexp.search(line):
                    self.normalize(in_file)
                    break

    # examplelib is already multi-threaded and will peg the whole
    # compute node.  These tasks should run sequentially.
    # When tasks are created, Arvados-specific objects like Collection file
    # objects are serialized as task parameters.  CrunchJob instantiates
    # these parameters as real objects when it runs the task.
    @CrunchJob.task(parallel_with=[])
    def normalize(self, coll_file):
        output = examplelib.frob(coll_file.mount_path())
        # self.output is a CollectionWriter.  When this task method finishes,
        # CrunchJob checks if we wrote anything to it.  If so, it takes care
        # of finishing the upload process, and sets this task's output to the
        # Collection UUID.
        with self.output.open(filename) as out_file:
            out_file.write(output)

if __name__ == '__main__':
    NormalizeMatchingFiles(task0='grep_files').main()
Notes/todo:
  • Important concurrency limits that job scripts must be able to express:
    • Task Z cannot start until all outputs/side effects of tasks W, X, Y are known/complete (e.g., because Z uses WXY's outputs as its inputs).
    • Task Y and Z cannot run on the same worker node without interfering with each other (e.g., due to RAM requirements).
  • In general, the output name is not known until the task is nearly finished. Frequently it is clearer to specify it when the task is queued, though. We should provide a convenient way to do this without any boilerplate in the queued task.
  • It would be nice to pass an openable object to a task rather than a filename. (i.e., grep_file() shouldn't have to repeat job_param('input'); that should be implicit in its argument.)
  • A second example that uses a "case" and "control" input (e.g., "tumor" and "normal") might help reveal features.
  • Should get more clear about how the output of the job (as opposed to the output of the last task) is to be set. The obvious way (concatenate all task outputs) should be a one-liner, if not implicit. Either way, it should run in a task rather than being left up to crunch-job.

Updated by Brett Smith over 9 years ago · 4 revisions