ishmael
ishmael

Reputation: 1906

Write to existing named pipe via Python

I have a named pipe, created in Bash as follows:

PIPE_IN=$(mktemp -u)
mkfifo $PIPE_IN

This produces, for example: /tmp/tmp.H8EP7rYjCL

How do I write to this named pipe in Python? I've tried re-opening this as a file:

with open('/tmp/tmp.H8EP7rYjCL', 'w') as f:
    f.write('something')

but that doesn't work. Oddly, it seems that whatever I write to the newly opened file gets buffered internally. When I write to the named pipe via Bash, I see the buffered content in the process attached to the named pipe. For example,

echo "foo" > /tmp/tmp.H8EP7rYjCL

yields

somethingfoo

Upvotes: 2

Views: 8577

Answers (1)

llogiq
llogiq

Reputation: 14541

You need to either flush the pipe or write a newline (which will usually flush automatically. This is what echo does, incidentially. Also read from the pipe before you kill the python process; otherwise it may be blocked on writing on the pipe.

Edit: Forget about flushing (see comment below).

Upvotes: 3

Related Questions