luispedro / jug

Parallel programming with Python
https://jug.readthedocs.io
MIT License
412 stars 62 forks source link

IterateTask as a decorator #85

Closed justinrporter closed 2 years ago

justinrporter commented 2 years ago

I have been using a decorator to provide the functionality of iteratetask instead of the function.

The reason I like this is that it specifies how to handle the output (i.e. how many items in the tuple) closer to the place the output is specified (i.e. at the end of the function) rather than where the workflow is specified (which is usually a different file).

So it looks like:

@IterateTask(n=3)
@jug.TaskGenerator
def do_thing(args):
    return a, b, c

I've been using the following for a few weeks without incident:

import jug
from functools import wraps

def IterateTask(n):
    def decorator(f):
         @wraps(f)
         def wrapper(*args, **kwargs):
             return jug.iteratetask(f(*args, **kwargs), n=n)
         return wrapper
    return decorator

Is this something you'd be interested in adding? Is there any reason I haven't foreseen that this isn't a good idea?

luispedro commented 2 years ago

This seems like a good idea. If you could open a PR, I'd be happy to accept (only thing missing is some documentation and a test, AFAIK; but I can also help with that)

justinrporter commented 2 years ago

Wait is this what return_tuple does? I happened across it when I was adding my decorator to task.py (where iterate_task is located).

luispedro commented 2 years ago

Yes, I had also forgotten about the existence of return_tuple, but it does seem to already be there

luispedro commented 2 years ago

I think we can close it here as the functionality is present as return_tuple (although, I would agree that the API is not very consistent in naming)

justinrporter commented 2 years ago

Agreed!