I'm pretty new to Erlang, however I've been trying to create a simple client server implementation. The server would hypothetically create worker processes to do "heavy work" in a "database" and then return the calculated values to the client.
My current steps are:
- Create a server process.
- Spawn a worker.
- Send the worker some work to do based off the clients input. (Here's where I'm confused)
- Send the data from the worker back to the client.
Here is some sample code.
-module(server).
-compile(export_all).
server() ->
receive
{From, {client, Name}} ->
io:format("Server has received request for ~p from ~p~n", [Name, From]),
Worker = spawn(server, getNameFromDataBase(self()),[]),
Worker ! Name,
From ! LastName,%%data returned from worker
server();
{database, LastName} ->
Data = LastName,
server()
end.
getNameFromDataBase(Server_Address) ->
receive
{name, Name} ->
timer:sleep(5000), %doing difficult work!
Server_Address ! {database, "Johnson"}
end.
client(Server_Address) ->
Server_Address ! {self(), {client, "Jim"}},
receive
{server, LastName} ->
io:format("Server got person's last name~p~n", LastName)
end.
How do you make it so that the data you receive from the worker will be seen and be able to be sent to the client?