用c语言编写的循环/环形缓冲区
pyringbuf的Python项目详细描述
一个python扩展,将字符的循环/环形缓冲区实现为c 分机。它无声地覆盖。
可用性
目前,pyringbuf可以在https://pypi.python.org/pypi/pyringbuf, 或者通过pip install pyringbuf。因为这是C分机,所以有一个 编译步骤,因此您的系统将需要能够编译扩展 python的模块。
用法
>>> from ringbuf import RingBuffer >>> R = RingBuffer(5) #choose your buffer size >>> R.push("a") #push a single character into the buffer >>> R.pop() #pop a single character 'a' >>> R.write("bcdef") #fill buffer with many characters at once >>> R.read(4) #read many characters at once 'bcde' >>> R.read(1) 'f' >>> R.read(1) #returns an empty string if the buffer is empty ''
实施
对于感兴趣的人来说,这已经实现为一个纯c扩展。戒指 缓冲区本身实现为固定长度的字符数组(8位有符号)。 缓冲区实例化时在python堆上动态分配。阅读和 写索引都是单调增加的,数组中的地址是 使用index%(模)buffer size确定。写得比缓冲区还多 大小立即引发异常,否则如果写入超过读取 模空间中的索引缓冲区内容将被自动覆盖。