[erlang-questions] open_port issue

Jayson Vantuyl <>
Wed Dec 9 11:54:59 CET 2009


It looks like the problem is that the port doesn't close the STDIN after writing the data.  I tried it with "cat" and got data back, but it never exited.  I eventually tested this from the command line and it looks like ImageMagick wants EOF.

There doesn't seem to be a port_control for this.  Obviously, this is bad.

Also, does {spawn_executable,Command} work for anyone?  I can't seem to get it to work.  I ALWAYS get badarg, even when {spawn,_} works.

On Dec 8, 2009, at 9:59 AM, Senthilkumar Peelikkampatti wrote:

> I wanted to call imagemagick's convert utility.
> 
> Here Cmd is ="convert - -resize 100x100 jpg:-", this command tells image
> magic to read the binary data from Stdin and emit the result back to stdout,
> but I am not error "Unable to write back to output". Is this the correct way
> to use open_port?
> 
> 
> cmd(Cmd, Bin) ->
> 
>    Port = open_port({spawn, Cmd}, [stream, use_stdio, in, out, binary]),
> 
>    port_command(Port, Bin),
> 
>    TRef = erlang:start_timer(3000, self(), timeout),
> 
>    recv_data(Port, TRef, <<>>).
> 
> 
> recv_data(Port, TRef, Buf) ->
> 
>    receive
> 
> {Port, {data, Bytes}} ->
> 
>    NewBuf = <<Buf/binary, Bytes/binary>>;
> 
> {Port, {data, _}} ->
> 
>    return(Port, TRef, {error, efbig});
> 
> {Port, eof} when Buf /= <<>> ->
> 
>    return(Port, TRef, {ok, Buf});
> 
> {Port, eof} ->
> 
>    return(Port, TRef, {error, enodata});
> 
> {timeout, TRef, _} ->
> 
>    return(Port, TRef, {error, timeout})
> 
>    end.
> 
> 
> return(Port, TRef, Result) ->
> 
>    case erlang:cancel_timer(TRef) of
> 
> false ->
> 
>    receive
> 
> {timeout, TRef, _} ->
> 
>    ok
> 
>    after 0 ->
> 
>    ok
> 
>    end;
> 
> _ ->
> 
>    ok
> 
>    end,
> 
>    catch port_close(Port),
> 
>    Result.
> 
> -- 
> Regards,
> Senthilkumar Peelikkampatti,
> http://pmsenthilkumar.blogspot.com/



-- 
Jayson Vantuyl








More information about the erlang-questions mailing list