KDB Inter-Process communication¶
Initializing an engine¶
Rayforce-Py allows you to access KDB databases using a seamless IPC client.
The connection with KDB database is being established via a dedicated KDBEngine type, which holds all connections to the specific KDB instance.
To open the connection, first import the KDBEngine into your runtime:
Then initialize the engine for a specific domain and port:
This will create a new KDB engine instance with a pool_size tracker which helps you to keep track over the opened connections with the KDB instance:
Opening the connection¶
There are 2 ways to open the connection with the KDB instance once the engine is initialized.
-
First way is to manually open a connection via the
Right after that the connection be executed (see below) or disposed:.acquire()method of the engine: -
Second way is to use the context manager, which handles the disposal for you, leaving no open connection outside of the actual manager:
Executing a query¶
In order to execute a KDB query, the .execute() method has to be called with the actual query being passed as an argument:
>>> with engine.acquire() as conn:
... result = conn.execute("x: 150; y: 150; x + y")
>>> print(result)
I64(300)
Rayforce-Py supports executing a query over any type, including tables:
>>> with engine.acquire() as conn:
... result = conn.execute(
... "0!select sum ExecQty by Broker from "
... "([] Broker:`Bro1`Bro1`Bro2`Bro3; ExecQty:404164 100000 9000 2900)"
... )
>>> result
Table[Symbol('Broker'), Symbol('ExecQty')]
>>> print(result)
┌────────┬────────────────────────────┐
│ Broker │ ExecQty │
│ SYM │ I64 │
├────────┼────────────────────────────┤
│ Bro1 │ 504164 │
│ Bro2 │ 9000 │
│ Bro3 │ 2900 │
├────────┴────────────────────────────┤
│ 3 rows (3 shown) 2 columns (2 shown)│
└─────────────────────────────────────┘