[erlang-questions] Treating data from open_port as an io_device()

Richard Carlsson <>
Mon Jun 30 23:23:30 CEST 2008


Tom Werner wrote:
> Can anyone recommend a way to treat the output from an open_port call as 
> an io_device() instead of as chunks of data that are delivered to the 
> port's controlling process and fetched via receive?
> 
> What I want is to use file:read/2 to read from the external process. The 
> reason is that data going TO the process already has 4 byte length 
> headers on it, so I can't use {packet, 4}. The data coming FROM the 
> process also has the headers so I'll need to do length header management 
> myself which is difficult (and inefficient) when dealing with chunked data.
> 
> Alternatively, is there a way to just create an in-memory io_device() 
> that I can use as a fifo? Then I could write the chunked data to the 
> fifo and read off the other side. I tried using a posix fifo file but 
> file:open/2 will not open it because of {error,eisdir}. It looks like I 
> can open the fifo as a port but that leaves me with the same exact problem.
> 
> I've looked all over the docs and asked in #erlang but I've seen no way 
> to create a simple pipe that I can use for this purpose!

No, this kind of functionality is missing from the standard libraries.
Right now, you have to roll your own I/O adapter process (you can base
it on the file lib/kernel/src/user.erl); it's not hard, but it's rather
boring, and there is no documentation. There have been discussions about
rewriting the I/O system, for efficiency and to make such things easier.

     /Richard



More information about the erlang-questions mailing list