Tutorial: Lightweight Processes
2 Processes in Modules
2.1 Shell spawn
: The Sequel
In the last section, we were all primed to explore spawning processes from the REPL. As we explored, we discovered that message passing in the REPL is a little cumbersome. You were also promised that it would be cleaner when we moved the code to modules. Let's see if that's true ...
Save the code below to messenger.lfe
:
Then start up lfe
, compile your new module, and spawn our print function:
Great! It works as expected. Now let's play... by sending it a message from the REPL:
The only problem with our solution is that it's a one-shot deal; subsequent sends to the pid won't call our function, since that function is no longer running. We can change that, though: let's make sure that once it prints the message, it starts listening again:
Let's take it for a test drive:
Horray! You've just written a simple listener in LFE!