[erlang-questions] buffered io:format in the shell

Serge Aleynikov saleyn@REDACTED
Tue May 22 14:07:52 CEST 2007


I modified the code to include spawning two processes (test1 and test2) 
that both write output to the shell.  When gen_server receives messages 
from the driver via handle_info and sends them to test1 they are not 
printed to the console until *any* key is pressed in the shell.  However 
when I send messages to 'test2' (i.e. test2 ! "some msg") the message 
gets printed immediately.

Any hint on what may be causing such an oddity?


test1() ->
     spawn(fun() -> register(test1, self()), loop() end),
     start_server().

test2() ->
     spawn(fun() -> register(test2, self()), loop() end),

loop() ->
     receive
     Msg ->
         io:format("~p: ~p\n", [self(), Msg]),
         loop()
     end.

start_server() ->
     gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

...

handle_info({Port, {data, Data}}, #state{port=Port} = State) ->
      (catch test1 ! binary_to_term(Data)},
      {noreply, State};

The driver generates messages via:

   driver_output(data->port, x.buff, x.buffsz);

 > Hi,
 >
 > I have a driver that delivers messages to a gen_server process using
 > driver_output function.  On the Erlang side the messages are received
 > in the form:

 > handle_info({Port, {data, Data}}, #state{port=Port} = State) ->
 >     case binary_to_term(Data) of
 >     {msg, Subj, [], Msg} ->
 >         io:format("To: ~s, Msg: ~s~n", [Subj, Msg]);
 >     Other ->
 >         error_log:error_msg("Unknown msg from port: ~p~n", [Other])
 >     end,
 >     {noreply, State};
 >
 >
 > The communication between the driver and Erlang process seems to work
 > fine with the exception that the result of io:format/2 call doesn't
 > get printed to the shell immediately after the message from driver is
 > received.  It gets printed only after I hit the <Enter> key on the
 > keyboard.  Any idea why?




More information about the erlang-questions mailing list