Skip to content

Adds a new API for delayed computations #15

@rafa-be

Description

@rafa-be

Adding a new decorator that executes the function in the background.

When a decorated function gets called, it returns immediately, and the computation will be executed in the background using the currently setup BackendEngine.

The return value of the function will behave like a regular value, except that it reading it will block until the computation finishes.

Example:

@delayed
def delayed_pow(a: float, b: float) -> float:
    return math.pow(a, b)

# This will compute all the `delayed_pow()` calls in parallel:
total_sum = sum([delayed_pow(x, 2) for x in range(0, 1000)])

# Operators should work too
print((delayed_pow(2, 2) + delayed_pow(4, 2)) * 4)

# Can be used without a decorator
a = delayed(math.sqrt)(16)
b = delayed(math.sqrt)(9)
print(a + b)

While being simpler to use that @parfun, it has a few disadvantages:

  • It does not try to find the optimal partitioning size of the parallelized tasks;
  • Exceptions are harder to understand, as these will be raised when the function return value is first accessed, not when then function is called.

As Pargraph already has a @delayed decorator, I'm thinking about using a different name. Here are some ideas:

  • @task;
  • @parasync;
  • @parallel_task;
  • @concurrent_task;

I already have a basic implementation working in new_delayed_api.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions