Lookup loop exhausts connections
Review this Python function that runs many independent lookups in a loop.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.
0:00 of about 22 min
Mark a line and say what kind of problem it is.0 findings
1def enrich(pool, ids):
2 results = {}
3 for id_ in ids:
4 conn = pool.getconn()
5 cur = conn.cursor()
6 cur.execute("SELECT name FROM users WHERE id = %s", (id_,))
7 row = cur.fetchone()
8 if row is None:
9 continue
10 results[id_] = row[0]
11 cur.close()
12 pool.putconn(conn)
13 return results
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.
Run or narrate your approach, then ask the coach.