File: vine_python_future_funcall.py

package info (click to toggle)
cctools 1%3A7.14.5-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 36,956 kB
  • sloc: ansic: 114,614; python: 29,532; cpp: 20,313; sh: 13,675; perl: 4,056; xml: 3,688; makefile: 1,436
file content (86 lines) | stat: -rw-r--r-- 2,102 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#! /usr/bin/env python

import sys
import ndcctools.taskvine as vine

port_file = None
try:
    port_file = sys.argv[1]
except IndexError:
    sys.stderr.write("Usage: {} PORTFILE\n".format(sys.argv[0]))
    raise


# Define a function to invoke remotely
def my_sum(x, y, negate=False):
    from operator import add, mul

    f = 1
    if negate:
        f = -1
    s = mul(f, add(x, y))
    return s


def failure():
    raise Exception('Expected failure.')


def main():
    executor = vine.FuturesExecutor(
        port=[9123, 9129], manager_name="test-executor", factory=False
    )
    print("listening on port {}".format(executor.port))
    with open(port_file, "w") as f:
        f.write(str(executor.port))

    # Create library task
    print("creating library from functions...")
    libtask = executor.create_library_from_functions(
        "test-library", my_sum, failure, hoisting_modules=None, add_env=False
    )

    # Install library on executor.manager
    executor.install_library(libtask)

    # Submit several tasks for execution
    t1 = executor.future_funcall("test-library", "my_sum", 7, 4)
    a = executor.submit(t1)

    t2 = executor.future_funcall("test-library", "my_sum", a, a)
    b = executor.submit(t2)

    t3 = executor.future_funcall("test-library", "my_sum", b, a)
    c = executor.submit(t3)

    print("waiting for result...")

    res = t3.output()
    print(f"t3 output = {c.result()}")

    a = 7 + 4
    b = a + a
    c = a + b
    assert res == c

    # Test failure handling
    f1 = executor.future_funcall("test-library", "failure")
    future = executor.submit(f1)

    try:
        future.result()
    except Exception as e:
        # FutureFunctionCall wraps the exception string in a
        # FunctionCallNoResult so we check the original error message is
        # present in the raised error.
        assert "Expected failure." in str(e)
        assert future.exception() is not None
    else:
        raise RuntimeError("Future did not raise exception.")


if __name__ == "__main__":
    main()


# vim: set sts=4 sw=4 ts=4 expandtab ft=python: