FIFO pipe only reads after write end has closed

前端 未结 3 1556
Happy的楠姐
Happy的楠姐 2021-01-15 05:52

I\'m trying to create a FIFO pipe between a python file and C file, but the issue is that when reading in the input from the C file, getline blocks until the writer end (in

3条回答
  •  盖世英雄少女心
    2021-01-15 06:28

    Your problem comes from the buffer. FIFO use block buffer by default. So the c program won't read anything until the write buffer of the fifo in python was full. And there's two way to change this behaviour:

    • specify the buffer mode:

    there's three buffer mode:

    1. block buffer(default)
    2. line buffer
    3. no buffer at all

    What meet your needs here is the line buffer, so use fifo = open("emg", "w", 1); instead of fifo = open("emg", "w"); will fix. number 1 here instead line buffer. python doc

    • another method is force flush the buffer, use fifo.flush after the write operation.

提交回复
热议问题