https://prefect.io logo
#data-tricks-and-tips
Title
# data-tricks-and-tips
g

Guilherme Bordignon

06/29/2022, 10:26 PM
hey everyone, I have this task that gets a list of every object for each model passed and return a tuple with each list,
Copy code
@async_task()
async def get_all_objects_from_models(
    models: List
):
    """
    Get all objects from each model from the @param models.
    """
    return await asyncio.gather(*[model.get_all_objects() for model in models])
to be used like this
Copy code
all_objects_model_1, all_objects_model_2, all_objects_model_3 = get_all_objects_from_models([Model1, Model2, Model3])
the problem is that I have to pass it a
nout
in the decorator or a
Tuple
in the return type annotation with the number of items inside (
Tuple[List, List, List]
) for this to work, otherwhise a got this error `'Task is not iterable. If your task returns multiple results, pass
nout
to the task decorator/constructor, or provide a
Tuple
return-type annotation to your task.',` but I would like to make this function generic and pass and return as many models as I want. Is there a way to make it work?
1
k

Kevin Kho

06/30/2022, 1:03 AM
Yeah this can’t be dynamic and you really need
nout
a

Anna Geller

06/30/2022, 11:53 AM
Is this for Prefect 1.0? Can you take a step back and first explain what you are trying to do?
y

Yupei Chen

06/30/2022, 1:32 PM
If you’re not committed to unpacking in the same step as the function call, you could just unpack the list after the function call.
👍 1
4 Views