Skip to content Skip to sidebar Skip to footer

Understanding Named Pipes (fifo) In Python

I am running Python 2.7 on a Unix environment (tested on Ubuntu and OSX) I have the following programs: With os.open(): [SCRIPT 1] import os pipe_1_name = 'pipe_1' pipe_2_name =

Solution 1:

What happens in the second set of scripts, the ones that don't use os.open(), Script 1 blocks at pipe_2 = open(pipe_2_name, 'r') while Script 2 blocks at pipe_2 = open(pipe_2_name, 'w').

No, Script 2 blocks at received = pipe_1.readline()[:-1].

Why is this happening?

It's because Script 1's open(pipe_1_name, 'w') causes the written message to be buffered in fixed-size chunks (typically 4096 or 8192 bytes), so the pipe_1.write("server_message_0\n") does not yet write anything to the pipe, but only to the buffer, and Script 2 doesn't get anything to read. See open() and also How often does python flush to a file?

To cure this, since your messages are complete lines, it suffices to use line buffering, e. g.

pipe_1 = open(pipe_1_name, 'w', 1)

(as well for the other write pipes).

Post a Comment for "Understanding Named Pipes (fifo) In Python"