使用Scapy Sniff(Python)的多线程队列效率低下
我不太擅长编程,希望我不要问愚蠢的问题。
我正在建立一个数据采集系统(DAQ),该系统在指定的接口上捕获数据包。简而言之,我构建了生产者 - 消费者模型,生产商负责捕获数据包并将数据存储在队列中,并且消费者负责负责从队列中获取数据并处理数据。
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()
最后,捕获的数据包的数量连续打印,但结果并不令人满意。重点和脱水的速度是如此之慢,以至于当我关闭信号源(不再发送数据包)时,仍然需要一段时间才能脱离队列的所有元素(仍在打印)。我想做的是实时同步,当我不再发送数据包时,队列中的元素也被脱水。 我想知道是什么原因导致了这一点,请原谅我对操作系统的熟悉程度不太熟悉,以及是否有可行的解决方案。
事先感谢您的帮助和建议!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论