|
| 1 | +--- |
| 2 | +title: Asynchronous Querying Example |
| 3 | +date: July 2025 |
| 4 | +author: KX Systems, Inc. |
| 5 | +tags: PyKX, q, asyncio, IPC, asynchronous |
| 6 | +--- |
| 7 | + |
| 8 | +# PyKX Calling into multiple q servers without blocking |
| 9 | + |
| 10 | +_This example provides a quick start for setting up a Python process using `PyKX` to call into |
| 11 | +multiple q servers without blocking each other._ |
| 12 | + |
| 13 | +To follow along, feel free to download this <a href="./archive.zip" download>zip archive</a> that |
| 14 | +contains a copy of the python scripts and this writeup. |
| 15 | + |
| 16 | +## Quickstart |
| 17 | + |
| 18 | +This example creates a python process that sends 2 queries meant to simulate long running queries to |
| 19 | +two separate q servers to show how to query q servers without blocking using `PyKX`. |
| 20 | + |
| 21 | +### Run the Example |
| 22 | + |
| 23 | +The example uses 2 servers opened up on ports 5050 and 5051, these servers can be opened with the |
| 24 | +commands `$ q -p 5050` and `$ q -p 5051` respectively. |
| 25 | +The script `async_query.py` can then be run to send the two queries simultaneously with |
| 26 | +`$ python async_query.py`. |
| 27 | + |
| 28 | +### Outcome |
| 29 | + |
| 30 | +The script will send the two queries and print their results followed by the total time taken |
| 31 | +by the script. |
| 32 | + |
| 33 | +The first query takes 10 seconds and the second takes 5 seconds to complete showing that both |
| 34 | +queries were processed without blocking eachother. |
| 35 | + |
| 36 | +```bash |
| 37 | +0 1 2 3 4 5 6 7 8 9 10 11 12 |
| 38 | +0 1 2 3 4 5 6 7 8 9 |
| 39 | +took 10.001731808 seconds |
| 40 | +``` |
| 41 | + |
| 42 | +### Important notes on usage of `QConnections` |
| 43 | + |
| 44 | +While the `#!python with` syntax for `QConnection` objects is useful for sending one shot requests it |
| 45 | +should be avoided where possible when repeatedly querying the same server. This is because |
| 46 | +connecting to q servers is blocking and the closing of `QConnection` objects is also blocking which |
| 47 | +will cause other queries to be delayed in certain cases. There is a simple class called |
| 48 | +`ConnectionManager` provided in this example to handle opening connections to servers and allow |
| 49 | +querying them by supplying a port alongside the query and any arguments. This class will also clean |
| 50 | +up the stored `QConnection` objects when its `#!python with` block ends, much like the normal |
| 51 | +`QConnection` objects do themselves. |
0 commit comments