Skip to content

hopen, hclose

hopen connect a process or file hclose disconnect a process or file

Kdb+ communicates with the file system and other processes through

  • one-shot functions
  • handles to persistent connections

Connections are opened and closed respectively by hopen and hclose.


hopen filehandle
hopen processhandle
hopen (communicationhandle;timeout)
hopen port


  • filehandle is a symbol atom (or string since V3.6 2017.09.26)
  • communicationhandle is a symbol atom (or string since V3.6 2017.09.26)
  • timeout is milliseconds as an integer
  • port is a local port number as an integer atom

conencts to a file object or a communication handle, and returns a connection handle as an int.

hopen ":path/to/file.txt"                   / filehandle
hopen `:unix://5010                         / localhost, Unix domain socket
hopen(":";10000)           / IP address and timeout
hopen 5010                                  / local port number

For IPC compatibility, it serializes to {hopen x}. e.g.

hopen each(`:mysymbol;


hclose x     hclose[x]

Where x is a connection handle, closes the connection, and destroys the handle. The corresponding integer can then no longer be applied to an argument.

q)show h:hopen `::5001
q)h"til 5"
0 1 2 3 4
q)hclose h
q)h"til 5"
': Bad file descriptor

Async connections: pending data on the connection handle is not sent prior to closing. If flushing is required prior to close, this must be done explicitly. (Since V3.6 2019.09.19)

q)neg[h][];hclose h;

hclose before V3.6 2019.09.19

If the handle refers to a WebSocket, hclose blocks until any pending data on the connection handle has been sent.


If a filehandle specifies a non-existent filepath, it is created, including directories.

q)hdat:hopen ":f.dat"             / data file (bytes)
q)htxt:hopen ":c:/q/test.txt"     / text file
Passing strings instead of symbols avoids interning of such symbols.

This is useful if embedding frequently-changing tokens in the username or password fields.

Do not use colons in a file-path. It conflicts with the pattern used to identify a process.

To append to these files, the syntax is the same as for IPC:

q)r:hdat 0x2324
q)r:htxt "some text\n"
q)r:htxt ` sv("asdf";"qwer")


Communication handles

A communication handle specifies a network resource, and may include authentication credentials for it. There are four forms.

host can be a hostname or IP address; omitted, it denotes the localhost
Unix domain socket
(Since V3.4.) Unix domain sockets can have significantly lower latency and higher throughput than a localhost TCP connection
Fifo/named pipe


On Unix builds since V3.4.

hopen `:                    / IP address
hopen ""                    / hostname
hopen `::5010                                / localhost
hopen 5010                                   / localhost
hopen `:unix://5010                          / localhost, Unix domain socket
hopen `:tcps://              / SSL/TLS with hostname
hopen (`;10000) / full arg list, 10s timeout

User and password are required if the server session has been started with the -u or -U command line options, and are passed to for (optional) additional processing.

The optional timeout applies to the initial connection, not subsequent use of it.

To send messages to the remote process:

q)h"2+2"          / synchronous (GET)
q)(neg h)"a:2"    / asynchronous (SET)

One-shot request

If only one synchronous query/request is to be run, then the one-shot synchronous request can be used to connect, send the query, get the results, then disconnect.

q)` "1+1"

It is more efficient to keep a connection open if there is an opportunity to re-use it for other queries.

.Q.Xf (create file)
Communication handle, Connection handle, File system, Interprocess communication
Client-server, Named pipes, SSL/TLS
Q for mortals §11.6.2 Opening a Connection Handle