Can messages be sent to processes interactively?
Craig Dickson
crd@REDACTED
Thu May 20 20:11:21 CEST 1999
James Hague wrote:
> I wrote a quickie test process so I can start it up and send messages to
> it interactively, just so I know that all is well:
>
> -module(sample).
> -export([start/0]).
>
> start() -> spawn(sample, server, []).
>
> server() ->
> receive
> stop -> ok;
> Msg ->
> io:fwrite("message received: ~w\n", [Msg]),
> server(N)
> end.
>
> This compiles just fine
No, it doesn't. Variable N is unbound, and no function server/1 is defined.
Furthermore, since you didn't export server/0, you can't spawn it.
If you really want to be sure (interactively) that your process has started,
have it output something when it starts up.
This ought to compile and work as you expect:
--- cut here ---
-module(sample).
-export([start/0, server/0]).
start() -> spawn(?MODULE, server, []).
server() ->
io:fwrite("server started\n"),
receive
stop -> io:fwrite("server stopping\n"), ok;
Msg -> io:fwrite("message received: ~w\n", [Msg])
end.
--- cut here ---
You should find that you can send messages to this server from the Erlang
shell.
Craig
More information about the erlang-questions
mailing list