Romain
04/28/2021, 10:43 AMmapped=True
when running from inside a mapped context``
As a dummy example, I'm trying to do something like this:
from prefect.tasks.core.function import FunctionTask
from prefect import Flow, apply_map
def task_block(z):
R = FunctionTask(lambda x: range(x))(z)
M = FunctionTask(lambda x: x + 1).map(R)
return M
with Flow('nested_map') as flow:
L = [1, 2, 3]
apply_map(task_block, L)
is there a way to do this kind of nested mapping differently?emre
04/28/2021, 12:00 PMflatten
is your friend.
https://docs.prefect.io/core/concepts/mapping.html#flat-mappingRomain
04/28/2021, 12:58 PMdef process_video(video):
chunks = GetChunksTask()(video) # return a list of tuple (first frame, last frame) for the chunks e.g. [(0, 24), (25, 50)]
chunked_videos = ChunkVideoTask().map(unmapped(video), chunks)
return chunked_videos
with Flow('nested_map') as flow:
videos = ['video_0.mp4', 'video_1.mp4', 'video_2.mp4']
apply_map(process_video, videos)
Does it make more sense now ?emre
04/28/2021, 1:49 PMflatten
# Make GetChunksTask return a tuple of (video_name, first_frame, last_frame)
# Note ChunkVideoTask needs to be slightly modified to accept a 3-tuple
from prefect import flatten
with Flow('budget_nested_map') as flow:
videos = ['0', '1']
chunks = GetChunksTask().map(video) # [[(0, 1, 10), (0,11, 20)], [(0, 1, 20), (1,21, 40)]]
chunked_videos = ChunkVideoTask().map(flatten(chunks))
This way each of your chunks keep a reference to the actual video, you won't need a nested structure to keep what video has what chunk.
Ofc you will need another step to reduce these back into video level, it is what it is 😅Kevin Kho
flatten
to map over a list of list type structure.Romain
04/28/2021, 1:56 PMKevin Kho
Romain
04/29/2021, 1:50 PMKevin Kho