Project

General

Profile

Python SDK » History » Version 3

Brett Smith, 08/28/2014 09:21 AM

1 1 Tom Clegg
h1. Python SDK
2
3
(design draft)
4
5 2 Tom Clegg
h3. Example crunch script
6
7 1 Tom Clegg
<pre><code class="python">
8
#!/usr/bin/env python
9
10
from arvados import CrunchJob
11
12
import examplelib
13
import re
14
15
class NormalizeMatchingFiles(CrunchJob):
16
    @CrunchJob.task()
17
    def grep_files(self):
18
        # CrunchJob instantiates input parameters based on the
19
        # dataclass attribute.  When we ask for the input parameter,
20
        # CrunchJob sees that it's a Collection, and returns a
21
        # CollectionReader object.
22 3 Brett Smith
        input_coll = self.job_param('input')
23
        for filename in input_coll.filenames():
24
            self.grep_file(self.job_param('pattern'), input_coll, filename)
25 1 Tom Clegg
26
    @CrunchJob.task()
27 3 Brett Smith
    def grep_file(self, pattern, collection, filename):
28
        regexp = re.compile(pattern)
29
        with collection.open(filename) as in_file:
30 1 Tom Clegg
            for line in in_file:
31
                if regexp.search(line):
32
                    self.normalize(filename)
33
                    break
34
35
    # examplelib is already multi-threaded and will peg the whole
36
    # compute node.  These tasks should run sequentially.
37
    @CrunchJob.task(parallel_with=[])
38
    def normalize(self, filename):
39
        output = examplelib.frob(self.job_param('input').mount_path(filename))
40
        # self.output is a CollectionWriter.  When this task method finishes,
41
        # CrunchJob checks if we wrote anything to it.  If so, it takes care
42
        # of finishing the upload process, and sets this task's output to the
43
        # Collection UUID.
44
        with self.output.open(filename) as out_file:
45
            out_file.write(output)
46
47
48
if __name__ == '__main__':
49
    NormalizeMatchingFiles(task0='grep_files').main()
50
</code></pre>
51 2 Tom Clegg
52
Notes/todo:
53
* Important concurrency limits that job scripts must be able to express:
54
** 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).
55
** Task Y and Z cannot run on the same worker node without interfering with each other (e.g., due to RAM requirements).
56
* 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.
57
* 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.)
58
* A second example that uses a "case" and "control" input (e.g., "tumor" and "normal") might help reveal features.
59
* 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@.