I'm not very good at programming, hope I'm not asking stupid questions.
I am building a data acquisition system (DAQ) which captures packets on the specified interface.In short, I built a producer-consumer model, the producer is responsible for capturing packets and storing the data in the queue, and the consumer is responsible for taking data from the queue and processing the data.
from scapy.all import *
from threading import Thread
from queue import Queue
class Sniffer(Thread):
"""Producer"""
def __init__(self, que):
super().__init__()
self.que = que
def run(self):
while True:
sniff(prn=self.que.put, iface=my_interface, filter=my_filter)
class Consumer(Thread):
"""Consumer"""
def __init__(self, que):
super().__init__()
self.que = que
self.channels = []
def run(self):
while True:
msg = self.que.get()
self.channels.append(msg)
print(len(self.channels))
que = Queue()
sniffer = Sniffer(que)
consumer = Consumer(que)
sniffer.start()
consumer.start()
Finally, the number of captured packets is continuously printed, but the result is not satisfactory. The speed of enqueuing and dequeuing is so slow that when I turn off the signal source (no more packets are sent), it still takes a while to dequeue all elements of the queue(still printing). And what I want to do is to synchronize in real time, when I no longer send packets, the elements in the queue are also all dequeued. I would like to know what is causing this, forgive me for not being very familiar with operating systems, and also if there is a feasible solution to this problem.
Thanks in advance for your help and advice!