I have a list of lists of strings, which are filenames:
chunks_list = [["file_1", "file_2"], ["file_3", "file_4", "file_5"], ...]
I need to process this files in celery tasks, so I have task which does it:
@celery_app.task
def process_file_task(filename):
# do some staff with file
# (e.g. produce data to Kafka)
An I need to run inner list of files parallel. And outer lists should be sequential.
The way of processing should be next:
worker1: file_1
worker2: file_2
The next group starts after the first is finished:
worker1: file_3
worker2: file_4
worker3: file_5
I tried to run my tasks in this way:
sequence_tasks = []
for chunks in chunks_list:
sequence_tasks.append(
group([process_file_task.si(filename) for filename in chunks])
)
tasks_chain = chain(
tasks_group for tasks_group in sequence_tasks
)()
tasks_chain.get()
The problem is that I could have thousands of files. And this way of running tasks consumes all my free memory, even if there are only hundred of them. Please tell me how to design my workflow in correct way to not to get out of memory.