0

Suppose that we receive data from TCP socket, and process it step by step to get the result. Each step is implemented as a function which tasks parameter from the previous and return result to the next. We chain all these functions as a callback chain and name each of these functions as f1 f2 ... fn.

There are no block functions in this callback chain and each callback runs pretty fast. However, the run time of the whole chain is not negligible, so run whole chain in single loop iteration is not accepted.

For simply chain these functions in one callback chain, it will run as:

data -> f1 -> f2 -> f3 -> f4 -> f5 ->... -> fn -> result
        |------- single loop iteration -------|

It is good to break this chain into many sections, and run each section in one single loop iteration. This looks like:

data -> f1 ->  f2 -> f3 -> f4 -> f5 ->... -> fn-1  -> fn -> result
        | loop1 |    |-- loop 2 --|          |- loop m -|

I know in Twisted, there exists a deferred() to do such a task. However, in libuv, how to do it?

Douglas Su
  • 3,214
  • 7
  • 29
  • 58

1 Answers1

0

libuv doesn't have a builtin API to do this. You do have some building blocks though: you could create some sort of "chain" structure where you remember the state, and run it in a uv_idle_t. Note that idle handles prevent the loop from blocking for i/o when they are active.

saghul
  • 1,990
  • 1
  • 13
  • 15