I am sending continuous stream of data from Arduino to my serial port at a high speed. I would like to dump those data to my hard drive continuously.
我正在高速地从Arduino向我的串口发送连续的数据流。我想将这些数据连续转储到我的硬盘上。
At low speed, a simple and inefficient code would do:
在低速时,一个简单而低效的代码可以:
import serial
ser = serial.Serial('COM4', baudrate=9600)
f = open('data.dat', 'wb')
for i in range(10000):
data = ser.read()
f.write(data)
f.flush()
ser.close()
f.close()
At higher speed, we can change data = ser.read()
to data = ser.read(10000)
so it would buffer more data in each function call, and therefore more efficient.
在更高的速度下,我们可以将data = ser.read()更改为data = ser.read(10000),这样它将在每个函数调用中缓冲更多数据,因此效率更高。
However, I am thinking: should there be a better way? Conceptually, I imagine that there is a way to buffer 10000 bytes of data, and in another thread/process to start saving this data to hard drive, and then go back to the main thread/process to keep receiving data.
但是,我在想:应该有更好的方法吗?从概念上讲,我想有一种方法可以缓冲10000字节的数据,并在另一个线程/进程中开始将这些数据保存到硬盘驱动器,然后返回主线程/进程以保持接收数据。
Would that be reasonable/possible? To be more specific, the questions are:
那会合理/可能吗?更具体地说,问题是:
1) Should I use multiple threads or processes?
1)我应该使用多个线程或进程吗?
2) Where should the data be stored and how should it be passed between threads/processes?
2)数据应存储在何处以及如何在线程/进程之间传递数据?
1 个解决方案
#1
There's no need. Disk writes are already dispatched. It has to be this way because disk devices don't have a way to write one byte to disk.
没有必要。已分派磁盘写入。必须采用这种方式,因为磁盘设备无法将一个字节写入磁盘。
#1
There's no need. Disk writes are already dispatched. It has to be this way because disk devices don't have a way to write one byte to disk.
没有必要。已分派磁盘写入。必须采用这种方式,因为磁盘设备无法将一个字节写入磁盘。