Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a new API for delayed computations #15

Open
rafa-be opened this issue Sep 20, 2024 · 2 comments
Open

Adds a new API for delayed computations #15

rafa-be opened this issue Sep 20, 2024 · 2 comments
Assignees

Comments

@rafa-be
Copy link
Collaborator

rafa-be commented Sep 20, 2024

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.

@rafa-be rafa-be self-assigned this Sep 20, 2024
@sharpener6
Copy link
Collaborator

sharpener6 commented Sep 22, 2024

@rafa-be,

better to give an example with very intensive but able to parallel computation, like map reduce, or for loop at the end does combine (reduce) them.

also we might need another decorator like @parfun that able to hold @delayed, but we can use the same decorator @parfun

@rafa-be
Copy link
Collaborator Author

rafa-be commented Sep 23, 2024

@sharpener6,

better to give an example with very intensive but able to parallel computation, like map reduce, or for loop at the end does combine (reduce) them.

I'm adding a more complex example in the tutorials 👍.

also we might need another decorator like @parfun that able to hold @delayed, but we can use the same decorator @parfun

I think it will be quite confusing. The behavior of these two decorators are highly different, and solve two distinct parallel problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants