Verpous
Verpous

Reputation: 746

Redirecting python script's stdin to fifo results in RuntimeError: input(): lost sys.stdin

I have this python script that's meant to function as a server which reads commands from stdin which is redirected to a fifo:

test.py:

while True:
    try:
        line = input()
    except EOFError:
        break

    print(f'Received: {line}')

In bash I run the commands:

mkfifo testfifo
test.py < testfifo &
echo word > testfifo

And instead of printing the line it received, the script quits with an error:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    line = input()
           ^^^^^^^
RuntimeError: input(): lost sys.stdin
[1]+  Exit 1                  test.py < testfifo

I've read some answers which say that there needs to be one writer to the fifo open at all times. So I tried to also run:

sleep 100 > testfifo &

Same error. I also tried replacing the input() line with this:

line = sys.stdin.readline()

And all that did was give me this error instead:

AttributeError: 'NoneType' object has no attribute 'readline'

I'm on Windows with mingw64, I suspect this may be a bug with mingw. Any ideas? Thanks.

EDIT: Since this problem seems rather hopeless and out of my control, I've opted for now to just use a UDP server instead. Bash supports redirecting to /dev/udp/host/port which makes this easy to do.

Upvotes: 1

Views: 48

Answers (0)

Related Questions