cancel
Showing results for 
Search instead for 
Did you mean: 

Non-blocking delayed execution

robsmith11
New Contributor
Suppose I have two functions:f:{system"sleep 0.1"; g[]}g:{show`hello}If I call f, g will execute in 100ms, but if I call f twice waiting 1msbetween each call (from another process via IPC), then g will executeonce after 100ms and then again after an additional 100ms.I'd prefer that the delay from the 2nd call to f is run in parallel, sothat g is called after 100ms and then again after only 1ms.Is there an easy way to do this in kdb?I could see scheduling events with .z.ts and \t 1, but that seems rathermessy and inefficient.Rob
1 REPLY 1

sieber
New Contributor
yes, you should use \t to set up timers.
you can use my timer implementation if you want:

(libaray/ k code)

k)T:([]t:`s#0#.z.Z;f:())
k)tn:.z.ts:{e:T i:t?&/t:T`t;$[0>r:.z.Z-0Wz^e`t;."\\t ",$0|-_r*86400000;[T::(i#T),(i+1)_T;@[e`f;::];tn[]]]}
k)t:{$[7h=t:abs@@x;x:.z.Z+19h$x;]; $[in[t]17 18 19h;x:"z"$x+.z.D+$[x< t$.z.Z;1;0];];  x:15h$x;   T,:`t`f!(x;y) ;tn[]; }


after this code you can use the timer function to set up a callback after a period of time (here echo hi after 1 sek)

t[1000; {1"hi"}]
t[1000; {1"hi"}]

or you can use  it with fixed times:

t[  .z.Z + 3%86400 ; {1"hi"}]
t[ 09:37; {1"hi"}]