Andy Irvine
11/19/2024, 7:50 PM@task
decorator to a function could dramatically increase the memory usage of a flow using the k8s worker and Prefect v3.1.1?
Two runs with the same parameters, image, k8s cluster show huge differences in memory when the function is labelled as at task. Appears to not be releasing memory and will max out any allowed memory (within reason) while the non task version will complete with well under 1/3 of the limitMarvin
11/19/2024, 7:50 PM@task
decorator in Prefect v3.1.1, it appears there's a potential for increased memory usage, especially when operating with Kubernetes workers.
From my search, while there isn't a specific open GitHub issue that matches your exact description, there are a few potential areas to explore based on Prefect's documentation:
1. Task Caching and Memory Use: The @task
decorator involves task caching which can affect memory usage. When tasks cache their results, it involves some storage and memory utilization. You can read more about task caching in the Prefect documentation on task caching.
2. Worker and Infrastructure Considerations: The Kubernetes worker, by definition, involves some management overhead. Prefect workers are designed to handle task lifecycles which can mean maintaining more state information or managing caches, potentially leading to increased memory use. Explore the worker deployment considerations in more depth here.
My advice would be to check if your tasks are inadvertently caching large datasets and adjust the caching policies or storage setups accordingly. Ensuring that your tasks are not persisting unnecessary data or are correctly cleaning up after execution might help mitigate memory bloat.
If you'd like a bit of a giggle amidst this debugging: think of these tasks as tiny software hoarders. They just can't seem to throw anything away!
Let me know if you need further assistance or a different query explored!Andy Irvine
11/19/2024, 8:20 PM@task(task_run_name=f"process_batch", persist_result=False)
With persist_result set to False the memory growth is still observed. The task is executed in loop (as it is for batches of 500 files) and the function itself needs to fetch those files from Google Cloud Storage. That is achieved with asyncio and a semaphore to limit to 20 concurrent downloads.
Could this use of asyncio be causing Prefect to hold onto references somehow? Profiling only appears to show weak references
data_batch = await asyncio.gather
Nate
11/19/2024, 8:33 PMAndy Irvine
11/19/2024, 8:34 PMNate
11/19/2024, 8:37 PMAndy Irvine
11/19/2024, 8:45 PMNate
11/19/2024, 8:48 PMAndy Irvine
11/19/2024, 8:48 PMAndy Irvine
11/19/2024, 8:48 PM@task
decorator makes things go crazy with memory:Nate
11/19/2024, 8:49 PMAndy Irvine
11/19/2024, 8:54 PMNate
11/19/2024, 8:55 PMAndy Irvine
11/20/2024, 3:02 PM