runtime
372 / 500
question 372 Threading & Multiprocessing · easy

Fix the lock that protects nothing

A page-view counter is updated from many web-server threads and still loses views under load, although record uses with and a lock. Find why the lock protects nothing and fix it.

Read and diagnose: this can't run in the browser, so you'll read real code and output and answer questions about it.

ClassPageViews(lock_factory=threading.Lock) with counts (a dictionary) and record(page)
record(page)adds 1 to counts[page] (missing pages start at 0) while holding the counter's lock
Rulethe counter creates one lock (with lock_factory()) and every update uses it

Loading your progress…

Fix the lock that protects nothing — Python practice · Runtime