Python 如何处理和保存来自传感器的连续数据
例如: 我在车上安装了一个传感器,它不断地发送数据,现在,我必须处理(融合)来自传感器的连续数据,但同时,当进程将完成其执行时,数据也将到来。因此,当进程需要时间执行时,如何存储即将到来的数据,以备将来使用Python 如何处理和保存来自传感器的连续数据,python,python-multiprocessing,python-multithreading,Python,Python Multiprocessing,Python Multithreading,例如: 我在车上安装了一个传感器,它不断地发送数据,现在,我必须处理(融合)来自传感器的连续数据,但同时,当进程将完成其执行时,数据也将到来。因此,当进程需要时间执行时,如何存储即将到来的数据,以备将来使用 sample code: buffer1=[] buffer2=[] def process_function(buffer): //processing while true: //data receiving
sample code:
buffer1=[]
buffer2=[]
def process_function(buffer):
//processing
while true:
//data receiving continously
buffer1.append(data)
if len(buffer1)>0: process(buffer1)
buffer2.append(data)
(while the process_function will take buffer1 to process, at the same time, the continuous data should be stored in buffer2 so that after finishing the process_function with buffer1 can process with buffer2 and repeat.)
您可以使用一个多处理队列和两个进程。一个用于生产者,一个用于消费者:
from multiprocessing import Process, Queue
def collection_sensor_values(mp_queue):
fake_value = 0
while True:
mp_queue.put(f"SENSOR_DATA_{fake_value}")
fake_value += 1
time.sleep(2)
def process_function(mp_queue):
while True:
sensor_reading = mp_queue.get(block=True)
print(f"Received sensor reading: {sensor_reading}")
q = Queue()
sensor_collector_process = Process(target=collection_sensor_values, args=(q,))
readings_process = Process(target=process_function, args=(q,))
all_procs = [sensor_collector_process, readings_process]
for p in all_procs:
p.start()
for p in all_procs:
# run until either process stops
if p.is_alive():
p.join()
for p in all_procs:
if p.is_alive():
p.terminate()
我建议你可以使用卡夫卡流媒体工具。您不需要使用缓冲区管理数据。你所需要的一切卡夫卡都会处理好的。我们需要的信息远不止这些。@Alexander Cécile请告诉我你需要什么。@Prabhuidhn关于传感器的信息,例如,@beyhan建议你为什么不使用卡夫卡。。这很直截了当。。有一个通过卡夫卡等待传感器信息的流程。。并从传感器向卡夫卡队列生成消息。。如果将来有多线程进程从同一队列中消费,Kafka将自己管理。。