-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemonstratingThreadAsCaller.py
35 lines (26 loc) · 1.46 KB
/
DemonstratingThreadAsCaller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#Code Snippet and Logic how Thread work as caller work. Let's divide into the part to understand logic and the whole concept.
import threading
from django.dispatch import Signal
#Define a signal
suspicious_traffic_detected_tac = Signal()
def alert_security_team(sender, **kwargs): #this is the sender of the signal, and This is a dictionary of keyword arguments
#function to get current thread ID
thread_id = threading.get_ident()
print(f"Security team alerted from thread ID - {thread_id}")
# now sending an alert to the security team
print("Alert has been sent - Suspicious traffic detected")
#Packet_data - This is a dictionary containing information about the network traffic.
def analyze_network_traffic(packet_data):
# Function to get the current thread ID
thread_id = threading.get_ident()
print(f"Analyzing network traffic from thread ID - {thread_id}")
#Now, Checking if the packet data is malicious
if packet_data["malicious"]:
# Send the signal
suspicious_traffic_detected_tac.send(sender=None)
#Connecting to the signal receiver
suspicious_traffic_detected_tac.connect(alert_security_team)
#Now, analyzing network traffic
packet_data = {"malicious": True}
analyze_network_traffic(packet_data)
#So, the output of the code will show that both the analyze_network_traffic function and the alert_security_team function have the same thread ID, hence it's shown that Django signals run in the same thread as the caller.