具有numpy/ctypes的环形缓冲区 [英] ring buffer with numpy/ctypes

查看:115
本文介绍了具有numpy/ctypes的环形缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个客户端,该客户端将通过tcp接收[EEG]数据并将其写入环形缓冲区.我认为将缓冲区作为ctypes或numpy数组可能会非常方便,因为可以在该缓冲区的任何位置创建一个numpy视图",并且无需任何复制操作即可读取/写入/处理数据.还是总的来说是个坏主意?

I'm developing a client which will receive the [EEG] data over tcp and write it to the ring buffer. I thought it can be very convenient to have the buffer as a ctypes or numpy array because it's possible to create a numpy 'view' to any location of such buffer and read/write/process the data without any copying operations. Or is it a bad idea in general?

但是,我看不到如何以这种方式实现固定大小的循环缓冲区.假设我创建了一个缓冲区对象,该对象在内存中是连续的.到达缓冲区末尾时,写入数据的最佳方法是什么?

However, I don't see how to implement a circular buffer of a fixed size this way. Suppose I have created a buffer object which is contiguous in memory. What is the best way to write the data when the end of the buffer is reached?

一种可能的方法是,当写指针到达缓冲区数组的末尾时,从头开始覆盖(已经很旧的)字节.但是,在这种情况下,无法创建边界块(用于处理)的numpy视图(或者可以吗?),因为在边界附近,因为其中一些块仍可以位于缓冲区数组的末尾,而另一个块可以位于缓冲区数组的末尾.它的开始.我读过不可能创建这样的圆形切片.该如何解决?

One possible way is to start overwriting the (already old) bytes from the begining when the write pointer reaches the end of the buffer array. Near the boundaries, however, the numpy view of some chunk (for processing) can't be created (or can it?) in this case, because some of it can still be located in the end of the buffer array while another already in its begining. I've read it's impossible to create such circular slices. How to solve this?

UPD::感谢大家的回答.如果有人也遇到相同的问题,请此处"是我得到的最终代码.

UPD: Thanks everybody for the answers. In case somebody also faces the same problem, here's the final code I've got.

推荐答案

如果需要一个N字节的窗口,请将缓冲区设置为2 * N字节,并将所有输入写入两个位置:i % Ni % N + N,其中i是一个字节计数器.这样,您在缓冲区中始终有N个连续的字节.

If you need a window of N bytes, make your buffer 2*N bytes and write all input to two locations: i % N and i % N + N, where i is a byte counter. That way you always have N consecutive bytes in the buffer.

data = 'Data to buffer'
N = 4
buf = 2*N*['\00']

for i,c in enumerate(data):
    j = i % N
    buf[j] = c
    buf[j+N] = c
    if i >= N-1:
        print ''.join(buf[j+1:j+N+1]) 

打印

Data
ata 
ta t
a to
 to 
to b
o bu
 buf
buff
uffe
ffer

这篇关于具有numpy/ctypes的环形缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆