Interprocess Communication (IPC) · Synchronous & Asynchronous Requests · Practice Question
Define and Call a Function on a Remote Process
Question
On the server, open a connection to the client and use a synchronous request to define the following function g on the client:
g:{x+y*z}Confirm the function was created on the client, then call it from the server with 3 arguments.
Solution
On the server:
q)\p 5001
q)clientH:hopen 5002
q)clientH(set;`g;{x+y*z})
`gOn the client (confirming):
q)g
{x+y*z}Calling it from the server:
q)clientH(`g;2;3;4)
14Alternative Solution
q)clientH({@[`.;`g;:;x]};{x+y*z})
`.Explanation
clientH(set;`g;{x+y*z}) sends a sync request whose function is set, called with g and {x+y*z} as its arguments. On the client this runs `g set {x+y*z}.
Once g exists, it can be called over IPC like any built-in: clientH(`g;2;3;4) evaluates g[2;3;4] (2+3*4=14) on the client and returns it synchronously.
The alternative defines g with amend instead of set: @[`.;`g;:;x] assigns x to g in the root namespace — functionally the same.