My question is if it is possible to directly open a (named) pipe using OpenRead
.
Introduction
In the docs of OpenRead, we have, under details and options,
If OpenRead does not succeed in opening a particular file or pipe, it generates a message, and returns $Failed.
suggesting that we can open a pipe with OpenRead
. However, we also have
On systems that support pipes, OpenRead["!command"] runs the external program specified by command, and opens a pipe to get input from it.
This last text adds to my suspicion that maybe it is only possible to open a stream indirectly. For example using something like OpenRead["!cat fifo"]
(this may actually be a terrible command, but you maybe you get the gist. I.e. it seems that cat sometimes "terminates" and but mostly it doesn't, so that the kernel gets stuck waiting for the command to finish).
However, this answer by rcollyer quite explicitly does what I have failed to do. It reads a fifo using OpenRead
.
My attempt
If I do (sorry about wasting your time with Pause, it is probably not necessary)
dir = FileNameJoin[{$TemporaryDirectory, "MMAIO"}];
CreateDirectory[dir]
Run["cd" <> " " <> dir <> " && mkfifo fifo"]
Pause[1]
OpenRead[FileNameJoin[{dir, "fifo"}]]
Then I simply get $Failed
and a message that it cannot be opened. Also when I connect some input to the fifo or when I just append some string to the fifo, it wont work.
Questions
Am I doing something wrong or is this just impossible? Might it depend on the system? I am using Mac OSX and version 9. Also, is there any other way to get input from a pipe directly, or do we have to resort to something like OpenRead["!cat fifo"]
?
Etc
This Q&A is related, but it is about MathematicaScript.
Background: The reason I bother with a fifo in the first place is to make a connection between Mathematica and other applications without writing on the hard disk. I am aware that MathLink might be something to look into.
OpenWrite
andWrite
/StringWrite
. $\endgroup$ – Paul Jul 4 '13 at 21:39