0
0
Djangoframework~8 mins

Signal dispatch process in Django - Performance & Optimization

Choose your learning style9 modes available
Performance: Signal dispatch process
MEDIUM IMPACT
This affects server response time and request handling speed by adding synchronous or asynchronous signal processing overhead.
Handling user creation events with Django signals
Django
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User
from myapp.tasks import async_heavy_task

@receiver(post_save, sender=User)
def trigger_async_task(sender, instance, **kwargs):
    # Delegate heavy work to async task
    async_heavy_task.delay(instance.id)
Offloads heavy processing to asynchronous background task, freeing request thread immediately.
📈 Performance GainRequest handling is fast; heavy work runs separately, reducing response delay.
Handling user creation events with Django signals
Django
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User

@receiver(post_save, sender=User)
def heavy_task(sender, instance, **kwargs):
    # Heavy synchronous processing
    import time
    time.sleep(5)  # Simulate long task
    print('User created:', instance)
The signal handler blocks the main thread for 5 seconds, delaying the HTTP response.
📉 Performance CostBlocks request handling for 5 seconds, increasing server response time.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Synchronous heavy signal handler0 (server-side)0 (server-side)0 (server-side)[X] Bad
Asynchronous signal handler with background task0 (server-side)0 (server-side)0 (server-side)[OK] Good
Rendering Pipeline
Signal dispatch runs during request processing on the server side before response is sent. Heavy synchronous handlers delay response generation.
Request Handling
Response Generation
⚠️ BottleneckSynchronous signal handlers blocking the main thread
Optimization Tips
1Avoid heavy synchronous processing inside Django signal handlers.
2Use asynchronous background tasks to handle time-consuming work triggered by signals.
3Monitor server response times to detect blocking caused by signal handlers.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of using synchronous signal handlers in Django?
AThey increase client-side rendering time
BThey cause layout shifts in the browser
CThey block the request thread, increasing response time
DThey increase CSS selector complexity
DevTools: Network
How to check: Open browser DevTools, go to Network tab, reload page and check server response time for requests triggering signals.
What to look for: Long server response time indicates blocking synchronous signal handlers.