Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Challenge: Profiling and Fixing a Leaky Pipeline | Profiling and Leak Detection
Python Memory Management
Sección 3. Capítulo 5
single

single

Challenge: Profiling and Fixing a Leaky Pipeline

Desliza para mostrar el menú

Tarea

Desliza para comenzar a programar

You are given a data processing script that has a memory problem. Your task is to use tracemalloc to measure allocations before and after an operation, then fix the leak using the tools covered in this section.

You are given the following leaky function:

report_cache = {}

def generate_report(report_id):
    if report_id not in report_cache:
        report_cache[report_id] = list(range(500))
    return report_cache[report_id]
  1. Import tracemalloc and functools.
  2. Start tracing with tracemalloc.start() and take a snapshot called snapshot_before.
  3. Call generate_report(report_id) for report_id in range(2000) in a loop.
  4. Take a second snapshot called snapshot_after and stop tracing with tracemalloc.stop().
  5. Compare the snapshots using compare_to("lineno") and store the result in top_stats. Print the first element of top_stats.
  6. Define a new function generate_report_fixed(report_id) decorated with @functools.lru_cache(maxsize=256) that returns list(range(500)).

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 5
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

some-alt