Bird
0
0

How can you ensure a signal handler runs only once per request even if the signal is sent multiple times?

hard📝 Application Q9 of 15
Django - Signals
How can you ensure a signal handler runs only once per request even if the signal is sent multiple times?
AConnect the handler with <code>dispatch_uid</code> set to None.
BUse a flag in the handler to track if it already ran during the request.
CDisconnect the handler after the first call automatically.
DUse the <code>pre_save</code> signal instead of <code>post_save</code>.
Step-by-Step Solution
Solution:
  1. Step 1: Understand repeated signal sends

    Signals can be sent multiple times per request, causing multiple handler calls.
  2. Step 2: Use a flag to limit handler execution

    By storing a flag (e.g., in thread-local storage), the handler can check if it already ran and skip repeats.
  3. Final Answer:

    Use a flag in the handler to track if it already ran during the request. -> Option B
  4. Quick Check:

    Flag inside handler prevents multiple runs [OK]
Quick Trick: Track handler calls with a flag to run once [OK]
Common Mistakes:
MISTAKES
  • Assuming dispatch_uid controls call count
  • Disconnecting handler causes missed signals
  • Switching signals doesn't limit calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes