open_port issue

Senthilkumar Peelikkampatti senthilkumar.peelikkampatti@REDACTED
Tue Dec 8 18:59:52 CET 2009


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/


More information about the erlang-questions mailing list