Skip to content

[Documentation] How to split a future object? #818

@jan-janssen

Description

@jan-janssen

When a function returns a tuple with multiple items, we want to have the capability access the individual items separately:

from executorlib import SingleNodeExecutor

def test_function(i):
    return "a", "b", i

with SingleNodeExecutor() as exe:
    future = exe.submit(test_function, 15)
    f1, f2, f3 = split(future=future, n=3)
    print(f1.result(), f2.result(), f3.result())

This can be realized with the following split() function:

from concurrent.futures import Future

class SplitFuture(Future):
    def __init__(self, future: Future, selector: int):
        super().__init__()
        self._future = future
        self._selector = selector

    def cancel(self):
        return self._future.cancel()

    def cancelled(self):
        return self._future.cancelled()

    def running(self):
        return self._future.running()

    def done(self):
        return self._future.done()

    def add_done_callback(self, fn):
        return self._future.add_done_callback(fn=fn)

    def result(self, timeout=None):
        return self._future.result(timeout=timeout)[self._selector]

    def exception(self, timeout=None):
        return self._future.exception(timeout=timeout)

    def set_running_or_notify_cancel(self):
        return self._future.set_running_or_notify_cancel()

    def set_result(self, result):
        return self._future.set_result(result=result)

    def set_exception(self, exception):
        return self._future.set_exception(exception=exception)


def split(future: Future, n: int):
    return [SplitFuture(future=future, selector=i) for i in range(n)]

While this functionality is not yet implemented, it is sufficiently simple to be defined dynamically.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions