0

I'm trying to read non-blocking from subprocess pipe on windows python3.

I made an implementation that pipes a FFMPEG video stream. It works on Linux but since FNCTL doesn't work on windows I'm looking at other ways of avoiding read/write blocking.

Since readline() isn't blocking the read stream this solution should work, however I need to recreate the read(bytes) function with readline(). I need to be able to read entire frames of size X (1920*1080*3) from the stream with readline. I'm guessing '\n' occurences needs to be handled aswell.

The code so far:

def enqueue_output(self, out, queue):  
        # This function should read(SIZE) from stream and add it to queue
        # IT DOES NOT CURRENTLY WORK
        temp = bytes()
        SIZE = 1920*1080*3
        while True:
            for line in iter(out.readline, b''):
                if len(temp) >= SIZE:
                    queue.put(line)
                temp += line

ffmpeg = sp.Popen(ffmpegCmd, stdin=sp.PIPE,
         stdout=sp.PIPE,stderr=sp.PIPE, close_fds=ON_POSIX, shell=True)

q = Queue()
t = Thread(target=self.enqueue_output, args=(ffmpeg.stdout, q))
t.daemon = True # thread dies with the program
t.start()

The Read function. Follow question, would it be better to have the byte assemble code in the read function?

img_thread = threading.Thread(target=self.reader,
                                    args=(q,))
            img_thread.start()
def reader(self, q): # TODO : catch exceptions
   s_buf = ''

        while True:
            # read line without blocking
            try:  line = q.get_nowait() # or q.get(timeout=.1)
            except Empty:
                print('no output yet')
            else: # got line
                print(line)
                try:
                    img = np.frombuffer(line, dtype=np.uint8)
                    img = img.reshape((1080, 1920, 3))
                except Exception as e:
                    print(e)
                    pass
                else:
                    cv2.imshow("test", img)
                    cv2.waitKey(1)

Some Example data of how it looks when i just send readline() :

b'\x05\x07\t\x06\x08\n'
b'\x05\x07\t\x06\x08\n'
b'\x07\t\x0b\x05\x07\t\t\x0b\r\x06\x08\n'
b'\x00\x02\x04\x01\x03\x05\x05\x07\t\x05\x07\t\x05\x07\t\x05\x07\t\x01\x03\x05\x00\x02\x04\t\x0b\r\x06\x08\n'
b'\x05\x08\x08\x05\x08\x08\x03\x06\x06\x01\x04\x04\x02\x05\x05\x06\t\t\x05\x08\x08\x05\x08\x08\x05\x08\x08\x05\x08\x08\x05\x08\x08\x05\x08\x08\x05\x08\x08\x05\x08\x08\x02\x04\x06\x05\x07\t\x05\x07\t\x02\x04\x06\x05\x07\t\x05\x07\t\x05\x07\t\x05\x07\t\x05\x07\t\x06\x08\n'
b'\x05\x07\t\x00\x00\x02\x07\t\x0b\x03\x05\x07\x00\x00\x00\x00\x00\x00\x03\x02\x04\x02\x01\x03\t\x08\n'
b'\x0c\x0b\r\x08\x07\t\x06\x05\x07\x04\x03\x05\x00\x00\x00\x05\x04\x06\x05\x04\x06\x05\x04\x06\x05\x04\x06\x06\x05\x07\x06\x05\x07\x06\x05\x07\x06\x05\x07\x06\x05\x07\x05\x04\x06\x06\x05\x07\x06\x05\x07\x05\x04\x06\x05\x04\x06\x05\x04\x06\x06\x05\x07\x05\x04\x06\x08\x07\t\t\x08\n'
b'\t\x08\n'
b'\x08\x07\t\x06\x05\x07\x05\x04\x06\x06\x05\x07\x08\x07\t\x02\x01\x03\x02\x01\x03\x08\x07\t\x06\x05\x07\x06\x05\x07\x06\x05\x07\x08\x07\t\x06\x05\x07\x06\x05\x07\x06\x05\x07\x06\x05\x07\x05\x04\x06\x05\x04\x06\x05\x04\x06\x05\x04\x06\x04\x04\x04\x05\x05\x05\x04\x04\x04\x04\x04\x04\x05\x05\x05\x05\x05\x05\x04\x04\x04\x05\x05\x05\x03\x03\x03\x03\x03\x03\x05\x05\x05\x06\x06\x06\x08\x08\x08\x06\x06\x06\x05\x05\x05\x02\x02\x02\x00\x00\x00\x04\x00\x03\x08\x04\x07\n'
b'\x06\t\x08\x04\x07\x07\x03\x06\x07\x03\x06\x07\x03\x06\x07\x03\x06\x07\x03\x06\x06\x02\x05\x05\x01\x04\x06\x02\x05\x06\x02\x05\x07\x03\x06\x08\x04\x07\x03\x03\x03\n'
b'\n'
b'\n'
b'\t\t\t\x06\x06\x06\t\t\t\x05\x05\x05\x04\x04\x04\t\t\t\x06\x06\x06\x05\x05\x05\x05\x05\x05\x06\x06\x06\x06\x06\x06\x08\x08\x08\x08\x08\x08\x08\x08\x08\x06\x06\x06\x06\x06\x06\x05\x05\x05\x04\x04\x04\x04\x04\x04\x05\x05\x05\x05\x05\x05\x05\x05\x05\x01\x01\x01\x05\x05\x05\x05\x05\x05\x02\x02\x02\x03\x03\x03\x08\x08\x08\x08\x08\x08\x04\x04\x04\x06\x03\x04\x06\x03\x04\x05\x02\x03\x05\x02\x03\x06\x03\x04\x06\x03\x04\x05\x02\x03\x05\x02\x03\x05\x02\x03\x06\x03\x04\x06\x03\x04\x05\x02\x03\x05\x02\x03\x05\x02\x03\x05\x02\x03\x04\x01\x02\x01\x01\x01\x00\x00\x00\x03\x03\x03\x05\x05\x05\x06\x06\x06\x08\x08\x08\x06\x06\x06\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x04\x04\x04\x05\x05\x05\x05\x05\x05\x05\x05\x05\x06\x06\x06\x06\x06\x06\x05\x05\x05\x05\x05\x05\x04\x04\x04\x04\x04\x04\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x06\x06\x06\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x04\x04\x04\x04\x04\x04\x03\x03\x03\x03\x03\x03\x06\x06\x06\x08\x08\x08\x02\x02\x02\x05\x05\x05\r\r\r\x11\x11\x11\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x07\x04\x05\x06\x03\x04\x06\x03\x04\x04\x01\x02\x06\x03\x04\x0b\x08\t\x08\x05\x06\x04\x01\x02\x07\x04\x05\x06\x03\x04\x06\x03\x04\x06\x03\x04\x06\x03\x04\x06\x03\x04\x06\x03\x04\x06\x03\x04\x04\x04\x04\x04\x04\x04\x00\x03\x03\x00\x03\x03\x00\x03\x03\x00\x03\x03\x03\x06\x06\x03\x06\x06\x02\x05\x05\x02\x05\x05\x01\x04\x04\x01\x04\x04\x01\x04\x04\x01\x04\x04\x04\x04\x04\x04\x04\x04\x05\x02\x03\x05\x02\x03\x05\x02\x03\x05\x02\x03\x06\x03\x04\x06\x03\x04\x06\x03\x04\x08\x05\x06\x08\x05\x06\x08\x05\x06\x04\x01\x02\x04\x01\x02\x05\x02\x03\x06\x03\x04\x07\x04\x05\x07\x04\x05\x05\x05\x05\x05\x05\x05\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x04\x04\x04\x06\x06\x06\x05\x05\x05\x04\x04\x04\x03\x03\x03\x04\x04\x04\x05\x05\x05\x05\x05\x05\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x01\x04\x04\x01\x04\x04\x01\x04\x04\x00\x03\x03\x02\x05\x05\x00\x01\x01\x00\x01\x01\x02\x05\x05\x03\x06\x06\x02\x05\x05\x00\x02\x02\x00\x03\x03\x00\x03\x03\x01\x04\x04\x02\x05\x05\x02\x05\x05\x01\x03\x05\x01\x03\x05\x00\x01\x06\x00\x01\x06\x01\x02\x07\x01\x02\x07\x01\x02\x07\x02\x03\x08\x02\x03\x08\x02\x03\x08\x00\x01\x06\x00\x01\x06\x01\x04\x04\x01\x04\x04\x01\x04\x04\x02\x05\x05\x02\x05\x05\x03\x06\x06\x02\x05\x05\x02\x05\x05\x00\x03\x03\x00\x03\x03\x00\x03\x03\x00\x03\x03\x00\x03\x03\x00\x03\x03\x00\x02\x02\x00\x02\x02\x00\x01\x01\x00\x02\x02\x02\x05\x05\x01\x04\x04\x04\x04\x04\x03\x03\x03\x04\x04\x04\x03\x03\x03\x04\x04\x04\x04\x04\x04\x02\x05\x05\x02\x05\x05\x01\x04\x04\x05\x08\x08\x07\x0c\x0c\x06\x0b\x0b\x04\t\t\x06\x0b\x0b\x05\n'
b'\n'
b'\x00\x02\x02\x03\x08\x08\x03\x08\x08\x03\x08\x08\x03\x08\x08\x03\x08\x08\x03\x08\x08\x01\x06\x06\x01\x06\x06\x03\x08\x08\x03\x08\x08\x04\t\t\x04\t\t\x03\x08\x08\x03\x08\x08\x03\x08\x08\x03\x08\x08\x04\t\t\x04\t\t\x04\t\t\x04\t\t\x04\t\t\x04\t\t\x04\t\t\x04\t\t\x03\x08\x08\x03\x08\x08\x03\x08\x08\x03\x08\x08\x04\t\t\x04\t\t\x04\t\t\x05\n'
b'\n'
b'\t\x0c\x0c\t\x0c\x0c\x03\x06\x06\x02\x05\x05\x03\x06\x06\x06\t\t\t\x0c\x0c\x08\x0b\x0b\x08\x0b\x0b\x06\t\t\x05\x08\x08\x02\x05\x05\x00\x03\x03\x03\x06\x06\x06\t\t\x03\x06\x06\x06\t\t\x01\x04\x04\x01\x04\x04\x05\x08\x08\x06\t\t\x02\x05\x05\x05\x08\x08\n'
b'\r\r\x02\x05\x05\x06\t\t\x08\x0b\x0b\x06\t\t\x05\x08\x08\x07\n'

Any help would be appreciated!

If anyone has a better advice on how to avoid the read/write block on subprocess for windows that would be great too.

Grebtsew
  • 192
  • 5
  • 13
  • 1
    You should never use `readline()` to read binary files because they do not have lines. – DYZ Feb 29 '20 at 04:14
  • I solved it for now by using WSL in windows to run a linux environment instead. But please let me know if there is a good way to read images from pipes non-blocking in windows. If there is some win32api call like fcntl. – Grebtsew Feb 29 '20 at 10:57

0 Answers0