One of the cool things about “newLisp” is that despite its small fooprint, it comes with a lot of functionality built-in. For instance, if you are interested in distributed computing, it is pretty easy to get started. In this article, I will touch upon the net-eval function that allows an expression to be evaluated on a remote computer.
I am using Windows version of “newLisp”, but the same examples should work on any other supported platform as well.
For convenience, I am running the server processes on the same machine, but in a real scenario, we will be using different computers.
Let us start two TCP/IP servers listening on two different ports:
Here I am using the ports 10000 and 12345, but you can use any allowed port number.
Let us start another instance of “newLisp”, which will act as the client. Enter the following commands:
The first argument to “net-eval” is the remote address. Since the servers are running on the same machine, I am using “localhost”. Otherwise it will be the actual IP address of the remote computer. The second argument is the port where the server is listening. The next argument is the expression that we would like to evaluate on the remote computer. In the above example I use the “map” function to transform an integer list.
The “net-eval” function optionally takes two more arguments – a timeout limit and a callback function, but I am not going to use them in this article.
Instead of using “net-eval” directly, let us define a simple wrapper function that only requires the expression to be evaluated. Again, for convenience, we will define two such functions for the two different servers:
Here are some sample commands using the do-remote functions:
Take a moment to understand the third command above. Here we are defining a new function called “foo” that will become available on the remote computer for future computations. This is one way of injecting new code into the remote machine(s). The last two expressions use this newly defined function on the server.
What if we try to invoke this function on the other server? Clearly, it will fail:
This demonstrates that supporting the correct environment for executing the remote expression is the responsibility of the server. Either it must be fully setup initially, or it must be updated dynamically, as we did in our example.
Let us now define a function “bar” dynamically on the second computer and use it:
As you see, we can now perform any arbitrary computation that involves three environments – the local client and the two remote servers. That is the power of distributed computing! Ideally, we should be using multithreading on the local machine to take advantage of such “parallel” execution where possible, but that is a different story.
Well, this article was intended to give you a glimpse of the “net-eval” function. Do take a look at the official manual for more information.
Have a nice weekend!
Recent Comments