6

I need to kill a remote KDB+ session. This can be done in several ways but I'd prefer to use IPC handlers.

I start a KDB+ session:

$ q -p 5000
KDB+ 3.0 2012.11.13 Copyright (C) 1993-2012 Kx Systems

Then I start another KDB session and I manage to kill the server successfully:

$ q
KDB+ 3.0 2012.11.13 Copyright (C) 1993-2012 Kx Systems

q)h: hopen `::5000
q)h(exit;0)
'close
q)\\

But, if I create a script (test.q) with the instructions above, it fails:

$ cat test.q 
h: hopen `::5000
h(exit;0)
\\

$ q test.q 
KDB+ 3.0 2012.11.13 Copyright (C) 1993-2012 Kx Systems

k){0N!x y}
'close
@
"q"
"h(exit;0)"
q))

Any ideas? I really appreciate.

nunaxe
  • 1,432
  • 2
  • 15
  • 16

4 Answers4

8

You are making a synchronous request to the remote server which means that you are expecting a response. The problem is that your request causes the remote server to shut down and close the connection immediately, resulting in an error and causing q to go into debug mode.

If you just want to send an exit to the remote server without causing an error, you can send the request asynchronously by using a negative value for the connection handle (notice the lack of the 'close error):

q)h: hopen `::5000
q)(neg h) (exit;0)
q)\\
moepud
  • 461
  • 2
  • 2
  • I had already tried an async call, no errors are thrown but it also doesn't shut down the server. Bear in mind the code should be run from a script and not in interactive mode. – nunaxe Feb 06 '13 at 22:23
  • That's strange; it seems to work just fine for me (on Windows). Using protected eval with a synchronous call will work, but also has the side effect of hanging your client while waiting for the remote process to shut down. This might be bad if you have a lot of processes to shut down. – moepud Feb 10 '13 at 21:02
4

I managed to sort this out by using Protected Evaluation:

In test.q file:

h: hopen `::5000
@[h; "exit 0"; {}]
\\
Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
nunaxe
  • 1,432
  • 2
  • 15
  • 16
0

You might want to try async. Also if needed you can try deferred async (neg h) ({exit 0};`)[]

Sujoy
  • 21
  • 2
0

You have option explicitly close session and discard handle:

h: hopen `:hostname:port  <BR>
h <BR>
h:hclose <BR>
h<BR>
kk.
  • 3,747
  • 12
  • 36
  • 67