alberto icon indicating copy to clipboard operation
alberto copied to clipboard

Fix creating new ports in erlang example

Open negativ opened this issue 10 years ago • 6 comments

Yours erlang example creates new port on every iteration. I think it's wrong and script should be fixed to the version like that:

#!/usr/bin/env escript
%%! -noshell -noinput
-mode(compile).
-define(ME, filename:basename(escript:script_name())).
-define(PRINT(STR, PARAMS), io:format("~s: " ++ STR ++ "~n", [?ME | PARAMS])).

loop(Port) ->
    port_command(Port, term_to_binary({"Hello world!", erlang:now()})),
    receive
        {_, {data, Data}} ->
            ?PRINT("~p", [binary_to_term(Data)]),
            loop(Port)
    end.

main([Bin]) ->
    Port = open_port({spawn, Bin},
                     [binary, {packet, 4}]),
    loop(Port);
main(_) ->
    io:format("~s", ["usage: echo.erl path/to/port"]).

There is no need to create hundreds of new ports.

negativ avatar Mar 05 '15 13:03 negativ

Thanks, one port is indeed enough. Mind submitting a PR?

superbobry avatar Mar 05 '15 14:03 superbobry

Yeah, ok. But i need some time to rework my local branch and then PR.

negativ avatar Mar 05 '15 14:03 negativ

Sure, no problem.

I've noticed you're working on Async integration: is it possible to support Async without duplicating the parsing?

superbobry avatar Mar 05 '15 14:03 superbobry

At first i thought to totally rewrite current Alberto module and make new one Alberto_async, but now i decided to create a functor Alberto_async.Make that wraps original functions in async-scheduler-friendly way. So there is no code duplication of term parsing (as i can see, original parsing functions cannot stuck the scheduler), but reading and writing from std i/o is totally rewriten. If you have some suggestions for this functionality just tell me and i'll refactor this part in alberto-way.

negativ avatar Mar 05 '15 17:03 negativ

Alberto I/O interface is fairly minimal. Most of the parsing code is isolated into pure functions. There's an example of Lwt integration in examples/, why don't we do the same for Async?

superbobry avatar Mar 05 '15 22:03 superbobry

I use alberto not only as an erlang-port library, primary it would be layer for close interaction with erlang message routers over tcp and udp, so there is some application-specific needs such as lazy evaluation of terms etc., wrapping original functions in deferred's is common api-style convertion in a project (graph-like db).

negativ avatar Mar 06 '15 09:03 negativ