Citi / parfun

Lightweight parallelisation library for Python
Apache License 2.0
8 stars 3 forks source link

Adds a new API for delayed computations #15

Open rafa-be opened 1 week ago

rafa-be commented 1 week ago

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:

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

I already have a basic implementation working in new_delayed_api.

sharpener6 commented 1 week ago

@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 commented 5 days ago

@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.