Getting elements from a NUL shared login in python

I have a situation in python where I need to loop elements from a stream separated by NUL specified in a format similar to find /somewhere -0 output

  • The stream is binary, elements can consist of all bytes except NUL
  • It is impossible to find out if all this will correspond to the available memory (suppose the thread can be infinite).
  • There is no way to find out the length of individual elements (suppose this can always be longer than READ_SIZE in my current solution below).

I feel like something is missing here, like fd.readlines() , but for \ 0 instead of \ n)

Here is what I am currently using to solve this problem:

 READ_SIZE = 2**14 def readitems(fd): buffer = b'' last = fd.read(READ_SIZE) while(last): x = last.split(b'\0') for i in range(len(x)-1): yield buffer + x[i] buffer = b'' buffer += x[-1] last = fd.read(READ_SIZE) 

If there really is no built-in method that I am missing for this, faster and / or cleaner solutions are welcome.

+2
source share

Source: https://habr.com/ru/post/1270544/


All Articles