import threading lock = threading.Lock() # Dictionary to track usage: {account_id: current_usage} active_streams_map = {} def increment_stream_count(account): with lock: current_usage = active_streams_map.get(account.id, 0) current_usage += 1 active_streams_map[account.id] = current_usage account.active_streams = current_usage account.save(update_fields=['active_streams']) def decrement_stream_count(account): with lock: current_usage = active_streams_map.get(account.id, 0) if current_usage > 0: current_usage -= 1 if current_usage == 0: del active_streams_map[account.id] else: active_streams_map[account.id] = current_usage account.active_streams = current_usage account.save(update_fields=['active_streams'])