0
0
Djangoframework~8 mins

Custom signals in Django - Performance & Optimization

Choose your learning style9 modes available
Performance: Custom signals
MEDIUM IMPACT
Custom signals affect the server-side processing time and can indirectly impact page load speed if they trigger heavy operations during request handling.
Triggering actions after a model save
Django
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.core.mail import send_mail
import threading

@receiver(post_save, sender=MyModel)
def async_task(sender, instance, **kwargs):
    threading.Thread(target=send_mail, args=(...)).start()
Runs heavy tasks asynchronously, freeing the main thread to respond faster.
📈 Performance GainNon-blocking signal handler, reduces server response delay significantly
Triggering actions after a model save
Django
from django.db.models.signals import post_save
from django.dispatch import receiver
import time

@receiver(post_save, sender=MyModel)
def heavy_task(sender, instance, **kwargs):
    # Heavy processing like sending emails or complex calculations
    time.sleep(5)  # Simulate delay
    print('Heavy task done')
Heavy processing inside signal blocks the request-response cycle, increasing server response time and slowing page load.
📉 Performance CostBlocks server response for 5+ seconds, increasing LCP indirectly
Performance Comparison
PatternServer ProcessingBlocking TimeImpact on LCPVerdict
Synchronous heavy signal handlerHigh CPU and I/OBlocks 5+ secondsDelays LCP[X] Bad
Asynchronous signal handler with threadingLow CPU main threadNon-blockingImproves LCP[OK] Good
Rendering Pipeline
Custom signals run on the server during request processing and do not directly affect browser rendering but can delay server response, impacting LCP.
Server Processing
Response Time
⚠️ BottleneckBlocking synchronous signal handlers that delay response
Optimization Tips
1Avoid heavy synchronous processing inside custom signal handlers.
2Offload long tasks to asynchronous workers or background threads.
3Monitor server response times to detect blocking signals.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of using synchronous custom signals in Django?
AThey increase browser rendering time directly
BThey block the server response, increasing page load time
CThey add extra CSS to the page
DThey cause layout shifts in the browser
DevTools: Network
How to check: Open DevTools, go to Network tab, reload page, and check server response time for requests triggering signals.
What to look for: Long server response times indicate blocking signal handlers slowing page load.