Efficient FIFO queue for arbitrarily sized chunks of bytes in Python

后端 未结 4 919
既然无缘
既然无缘 2021-02-05 09:19

How do I implement a FIFO buffer to which I can efficiently add arbitrarily sized chunks of bytes to the head and from which I can efficiently pop arbitrarily sized chunks of by

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-05 09:31

    I have currently implemented this with a StringIO object. Writing new bytes to the end of the StringIO object is fast, but removing bytes from the beginning is very slow, because a new StringIO object, that holds a copy of the entire previous buffer minus the first chunk of bytes, must be created.

    Actually the most typical way of implementing FIFO is two use wrap around buffer with two pointers as such:

    enter image description here image source

    Now, you can implement that with StringIO() using .seek() to read/write from appropriate location.

提交回复
热议问题