質問

For an application where each request may take a second or two, is it possible to only process a piece of operative code on each iteration of the event loop? For example:

function foo()
{
    ...operative code...
    ...start processing the next event here...
    ...continue with foo() here....
}

Would it be something like this?

function foo()
{
    ...operative code...
    process.nextTick(function() {
        ...continue with foo() here...
    });
}

And if that is the way to do it, does Node automatically start processing whichever event is next in the queue?

役に立ちましたか?

解決

If the time is spent in IO, node's non-blocking model will automatically handle concurrency.

If it's not spent in IO, then you're right in using process.nextTick to defer the execution of your code so that other requests have a change to be handled. Here is a good writeup:

http://howtonode.org/understanding-process-next-tick

他のヒント

Your assumption is correct, you should be dividing the work into smaller blocks of execution and schedule the next block with process.nextTick. All the other scheduled events that need to be executed at the next tick will be handled prior to your block of required execution.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top