|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "### Executing code in a remote process\n", |
| 8 | + "\n", |
| 9 | + "Suppose we wanted to use some functions defined in the following q process: " |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 1, |
| 15 | + "metadata": { |
| 16 | + "collapsed": false |
| 17 | + }, |
| 18 | + "outputs": [ |
| 19 | + { |
| 20 | + "data": { |
| 21 | + "text/plain": [] |
| 22 | + }, |
| 23 | + "execution_count": 1, |
| 24 | + "metadata": {}, |
| 25 | + "output_type": "execute_result" |
| 26 | + } |
| 27 | + ], |
| 28 | + "source": [ |
| 29 | + "/ Open another q process\n", |
| 30 | + "system $[.z.o like\"w*\";\"start \";],\"q rmt.q -p 4044\"" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "metadata": {}, |
| 36 | + "source": [ |
| 37 | + "The file ``rmt.q`` contains the following code:" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": null, |
| 43 | + "metadata": { |
| 44 | + "collapsed": true |
| 45 | + }, |
| 46 | + "outputs": [], |
| 47 | + "source": [ |
| 48 | + "/ run the cell to see the content of the file\n", |
| 49 | + "/%loadscript rmt.q" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "markdown", |
| 54 | + "metadata": {}, |
| 55 | + "source": [ |
| 56 | + "This allows to call the functions in the process either synchronously or asynchronously using a handle:" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": 2, |
| 62 | + "metadata": { |
| 63 | + "collapsed": false, |
| 64 | + "scrolled": true |
| 65 | + }, |
| 66 | + "outputs": [ |
| 67 | + { |
| 68 | + "data": { |
| 69 | + "text/plain": [ |
| 70 | + "5\r\n" |
| 71 | + ] |
| 72 | + }, |
| 73 | + "execution_count": 2, |
| 74 | + "metadata": {}, |
| 75 | + "output_type": "execute_result" |
| 76 | + } |
| 77 | + ], |
| 78 | + "source": [ |
| 79 | + "/ Open connection\n", |
| 80 | + "h:hopen`::4044\n", |
| 81 | + "\n", |
| 82 | + "/ Synchronous\n", |
| 83 | + "h\"add . 2 3\"\n", |
| 84 | + "/Asynchronous\n", |
| 85 | + "neg[h]\"add . 3 3\"" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "markdown", |
| 90 | + "metadata": {}, |
| 91 | + "source": [ |
| 92 | + "In addition, as the script ```jupyterq_remote.q``` is loaded in the remote process, ```/%remote handle``` allows to run code in the remote process easily:" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": 1, |
| 98 | + "metadata": { |
| 99 | + "collapsed": false |
| 100 | + }, |
| 101 | + "outputs": [], |
| 102 | + "source": [ |
| 103 | + "/%remote h\n", |
| 104 | + "hello:f(\"Hello\";\"World\")\n", |
| 105 | + "table:rndtab 100" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "markdown", |
| 110 | + "metadata": {}, |
| 111 | + "source": [ |
| 112 | + "And completion or help can be used as if the remote process was the server process the notebook is running:" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": null, |
| 118 | + "metadata": { |
| 119 | + "collapsed": false |
| 120 | + }, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "/%remote h\n", |
| 124 | + "ad / place your cursor anywhere in the word and hit tab\n", |
| 125 | + "tab / if you place your cursor in this word and hit TAB you should see a drop down with table and tables\n", |
| 126 | + "rndtab / if you place your cursor anywhere and hit SHIFT TA, you should obtain info about the function" |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "markdown", |
| 131 | + "metadata": {}, |
| 132 | + "source": [ |
| 133 | + "Variable ``hello`` in the remote process should contain the string \"Hello World\":" |
| 134 | + ] |
| 135 | + }, |
| 136 | + { |
| 137 | + "cell_type": "code", |
| 138 | + "execution_count": 3, |
| 139 | + "metadata": { |
| 140 | + "collapsed": false |
| 141 | + }, |
| 142 | + "outputs": [ |
| 143 | + { |
| 144 | + "data": { |
| 145 | + "text/plain": [ |
| 146 | + "1b\r\n" |
| 147 | + ] |
| 148 | + }, |
| 149 | + "execution_count": 3, |
| 150 | + "metadata": {}, |
| 151 | + "output_type": "execute_result" |
| 152 | + } |
| 153 | + ], |
| 154 | + "source": [ |
| 155 | + "\"Hello World\"~h\"hello\"" |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "markdown", |
| 160 | + "metadata": {}, |
| 161 | + "source": [ |
| 162 | + "And ``table`` is a table that we can query:" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "code", |
| 167 | + "execution_count": 2, |
| 168 | + "metadata": { |
| 169 | + "collapsed": false |
| 170 | + }, |
| 171 | + "outputs": [ |
| 172 | + { |
| 173 | + "data": { |
| 174 | + "text/plain": [ |
| 175 | + "time sym price \r\n", |
| 176 | + "-------------------------\r\n", |
| 177 | + "00:16:31.025 A 15.17091\r\n", |
| 178 | + "00:32:08.956 A 15.1598 \r\n", |
| 179 | + "01:15:49.472 A 14.11597\r\n", |
| 180 | + "01:23:51.162 A 10.83889\r\n", |
| 181 | + "01:28:03.675 A 11.95991\r\n", |
| 182 | + "01:42:57.587 A 13.75638\r\n", |
| 183 | + "02:01:41.131 A 16.13745\r\n", |
| 184 | + "03:13:29.907 A 19.49975\r\n", |
| 185 | + "03:28:48.269 A 15.75905\r\n", |
| 186 | + "04:15:16.827 A 10.81235\r\n", |
| 187 | + "04:53:08.684 A 19.44167\r\n", |
| 188 | + "05:28:08.747 A 10.13921\r\n", |
| 189 | + "05:31:41.221 A 17.14878\r\n", |
| 190 | + "05:38:18.182 A 11.94651\r\n", |
| 191 | + "06:44:20.731 A 19.32632\r\n", |
| 192 | + "07:02:54.903 A 10.57525\r\n", |
| 193 | + "07:16:54.852 A 12.56066\r\n", |
| 194 | + "07:24:52.364 A 11.02443\r\n", |
| 195 | + "07:29:31.107 A 18.6711 \r\n", |
| 196 | + "08:04:51.809 A 17.5201 \r\n", |
| 197 | + "..\r\n" |
| 198 | + ] |
| 199 | + }, |
| 200 | + "execution_count": 2, |
| 201 | + "metadata": {}, |
| 202 | + "output_type": "execute_result" |
| 203 | + }, |
| 204 | + { |
| 205 | + "data": { |
| 206 | + "text/plain": [ |
| 207 | + "sym| price \r\n", |
| 208 | + "---| --------\r\n", |
| 209 | + "A | 15.18263\r\n", |
| 210 | + "B | 14.2251 \r\n", |
| 211 | + "C | 15.23465\r\n" |
| 212 | + ] |
| 213 | + }, |
| 214 | + "execution_count": 2, |
| 215 | + "metadata": {}, |
| 216 | + "output_type": "execute_result" |
| 217 | + } |
| 218 | + ], |
| 219 | + "source": [ |
| 220 | + "/%remote h\n", |
| 221 | + "select from table where sym=`A\n", |
| 222 | + "select avg price by sym from table" |
| 223 | + ] |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "markdown", |
| 227 | + "metadata": {}, |
| 228 | + "source": [ |
| 229 | + "Or load in the notebook:" |
| 230 | + ] |
| 231 | + }, |
| 232 | + { |
| 233 | + "cell_type": "code", |
| 234 | + "execution_count": 4, |
| 235 | + "metadata": { |
| 236 | + "collapsed": false |
| 237 | + }, |
| 238 | + "outputs": [ |
| 239 | + { |
| 240 | + "data": { |
| 241 | + "text/plain": [ |
| 242 | + "time sym price \r\n", |
| 243 | + "-------------------------\r\n", |
| 244 | + "00:03:17.484 C 13.92752\r\n", |
| 245 | + "00:16:31.025 A 15.17091\r\n", |
| 246 | + "00:32:08.956 A 15.1598 \r\n", |
| 247 | + "00:33:10.867 B 14.06664\r\n", |
| 248 | + "00:44:37.591 C 11.78084\r\n", |
| 249 | + "00:53:08.324 B 13.01772\r\n", |
| 250 | + "00:53:31.660 C 17.85033\r\n", |
| 251 | + "01:04:18.632 B 15.3471 \r\n", |
| 252 | + "01:11:57.854 B 17.11172\r\n", |
| 253 | + "01:15:49.472 A 14.11597\r\n" |
| 254 | + ] |
| 255 | + }, |
| 256 | + "execution_count": 4, |
| 257 | + "metadata": {}, |
| 258 | + "output_type": "execute_result" |
| 259 | + }, |
| 260 | + { |
| 261 | + "data": { |
| 262 | + "text/plain": [ |
| 263 | + "time sym price \r\n", |
| 264 | + "-------------------------\r\n", |
| 265 | + "00:03:17.484 C 13.92752\r\n", |
| 266 | + "00:33:10.867 B 14.06664\r\n", |
| 267 | + "00:44:37.591 C 11.78084\r\n", |
| 268 | + "00:53:08.324 B 13.01772\r\n", |
| 269 | + "01:15:49.472 A 14.11597\r\n", |
| 270 | + "01:17:27.614 C 14.93183\r\n", |
| 271 | + "01:23:51.162 A 10.83889\r\n", |
| 272 | + "01:28:03.675 A 11.95991\r\n", |
| 273 | + "01:42:57.587 A 13.75638\r\n", |
| 274 | + "02:33:08.380 B 12.29662\r\n", |
| 275 | + "02:43:26.164 C 14.70788\r\n", |
| 276 | + "03:07:08.088 B 12.30638\r\n", |
| 277 | + "03:27:45.539 C 14.39081\r\n", |
| 278 | + "04:03:04.576 C 13.89056\r\n", |
| 279 | + "04:06:03.749 B 13.91543\r\n", |
| 280 | + "04:15:16.827 A 10.81235\r\n", |
| 281 | + "04:20:03.661 B 12.78212\r\n", |
| 282 | + "04:26:56.542 C 12.39234\r\n", |
| 283 | + "04:28:21.989 B 11.50813\r\n", |
| 284 | + "04:34:15.435 B 11.56732\r\n", |
| 285 | + "..\r\n" |
| 286 | + ] |
| 287 | + }, |
| 288 | + "execution_count": 4, |
| 289 | + "metadata": {}, |
| 290 | + "output_type": "execute_result" |
| 291 | + } |
| 292 | + ], |
| 293 | + "source": [ |
| 294 | + "10#t:h\"table\"\n", |
| 295 | + "select from t where price<15" |
| 296 | + ] |
| 297 | + }, |
| 298 | + { |
| 299 | + "cell_type": "code", |
| 300 | + "execution_count": 5, |
| 301 | + "metadata": { |
| 302 | + "collapsed": false |
| 303 | + }, |
| 304 | + "outputs": [], |
| 305 | + "source": [ |
| 306 | + "/ Close q process\n", |
| 307 | + "neg[h](exit;0);" |
| 308 | + ] |
| 309 | + } |
| 310 | + ], |
| 311 | + "metadata": { |
| 312 | + "kernelspec": { |
| 313 | + "display_name": "Q (kdb+)", |
| 314 | + "language": "q", |
| 315 | + "name": "qpk" |
| 316 | + }, |
| 317 | + "language_info": { |
| 318 | + "file_extension": ".q", |
| 319 | + "mimetype": "text/x-q", |
| 320 | + "name": "q", |
| 321 | + "version": "3.6.0" |
| 322 | + } |
| 323 | + }, |
| 324 | + "nbformat": 4, |
| 325 | + "nbformat_minor": 2 |
| 326 | +} |
0 commit comments