QforMortals3/Interprocess Communication101

From Kx Wiki
Jump to: navigation, search

1.19 Interprocess Communication101

For this section, you will need two open q sessions, best done on the same machine. We recommend that this machine be one that is not encumbered with enterprise security. Chose one session to be your “server” and open a port with the command \p (note lower case) followed by the port number. To verify that the port is open, execute the naked command \p and check that it echoes a 32-bit int of the port you opened.

q)\p 5042			/ on server
q)\p
5042i 

The syntax of Interprocess Communication (IPC) is similar to that of File I/O. A ‘’symbolic network handle’’ is a symbol of a particular form that identifies the name of a resource on the network. For our purposes, it suffices to consider a network handle of the simplest form.

`:localhost:5042

The leading ‘:’ in the symbol identifies it as a symbolic handle. To the left of the second ‘:’ is the name of the network resource—in this case, the machine on which the q session is running. To the right of ‘:’ is a (presumably open) port on the destination machine that will be used for TCP/IP communication.

To open a connection, use a symbolic handle as argument to (hopen) and store the result in a variable, traditionally called h. Do that now in your “client” session after ensuring that the specified port is open in the “server” session.

q)h:hopen `:localhost:5042		/ on client

The variable h is called an open handle. It holds a function for sending a request to the server and receiving the result of that request. Now we’re ready to party.

There are three ways to send requests from the client to the server, only one of which is safe for production applications. For demonstration purpose (only), we show the simplest, which should only be used in development environments. When invoked with a string—i.e., a list of char—argument, the handle function h synchronously sends that string to the server, where it is executed, and any result is returned from the application of h.

q)h "6*7"			/ on client
42

Clearly this isn’t safe, as arbitrary text can be sent for nefarious purposes.

A safer way to make requests to the server is to invoke h with a list containing the name of a function that (presumably) exists on the server, followed by arguments to that function. When h is invoked with such a list argument, it (synchronously) causes the server to apply the named function to the transmitted arguments, and then returns any result from the server is its own output. This corresponds to call by name in traditional remote procedure call. It is safer since the server can inspect the symbolic function name and determine whether the requesting user is authorized to execute it

On your server process, create a simple function of two arguments.

q)f:{x*y}			/ on server

On your client process, invoke h with a list containing the symbolic name of the remote function followed by its two arguments.

q)h (`f; 6; 7)		/ on client
_

Observe that nothing is displayed on the server console since the function application there returns its result to the client. To close the connection with the server, flush buffers and free resources, apply (hclose) to the open handle.

q)hclose h		/ on client

IPC doesn’t get any easier.



Prev: File I/O 101, Next: Example: Asynchronous Callbacks

Reprinted with the author's permission from: q for Mortals Version 3, An Introduction to Q Programming by Jeffry A. Borror.

The book is available on Amazon. In the United Kingdom, it is available at Amazon UK.

©2015 Jeffry A. Borror/ q4m LLC

Personal tools
Namespaces
Variants
Actions
Navigation
Print/export
Toolbox