Home > Blockchain >  Force killing of q session after query is done
Force killing of q session after query is done

Time:06-08

I am trying to force killing (not closing) a q session once my query is done to save resources on my machine.

It is currently working using:

conn.sendAsync("exit 0")

Problem is, if I run a query right after it again (trying to reopen the connection and run another query), it might fail as the previous connection would still being killed as it is asynchronous.

Therefore, I am trying to do the same thing with a synchronous query, but when trying:

conn.sendSync("exit 0")

I get:

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
python-BaseException

Can I specify a timeout such that the q session will be killed automatically after say 10 seconds instead, or maybe there is another way to force killing the q session?

My code looks like this:

conn = qc.QConnection(host='localhost', port=12345, timeout=10000)
conn.open()
res = None
try:
    res = conn.sendSync(query, numpy_temporals=True)
except Exception as e:
    print(f'Error running {query}: {e}')
conn.sendSync("exit 0")
conn.close()

CodePudding user response:

I'd suggest we take a step back and re-evaluate if it's really a right thing to kill the KDB process after your Python program runs a query. If the program isn't responsible to bring up the KDB process, most likely it should not bring the process down.

Given the rationale of saving resource, I believe it keeps many data in memory and thus takes time to start up. It adds another reason that you shouldnt kill it if you need to use it a second time.

CodePudding user response:

You shouldn't be killing a kdb process you intend to query again. Some suggestions on points in your question:

once my query is done to save resources -> you can manually call garbage collection with .Q.gc[] to free up memory or alternatively and perhaps better enable immediate garbage collection with -g 1 on start. Note if you create large global variables in your query this memory will not be freed up / returned.

https://code.kx.com/q/ref/dotq/#qgc-garbage-collect

https://code.kx.com/q/basics/syscmds/#g-garbage-collection-mode

killed automatically after say 10 seconds -> if your intention here is to not allow client queries such as from your python process to run over 10 seconds you can set a query timeout with -T 10 on start or when process is running with \T 10 / system "T 10"

https://code.kx.com/q/basics/cmdline/#-t-timeout

  • Related