But even more generally, if you do run a CPU intensive code, like the large 
for-loop, keep in mind that you WILL block the server from responding until 
the synchronous calculation is finished. For example, another request 
waiting to be serviced will have to wait, even if its own processing will 
be trivial. Therefore, you should not do synchronous long operations on the 
event-loop in node.js; there are other alternatives (spawning a separate 
process, having another dedicated node.js server, etc.).
About res.end with or without setTimeout or process.nextTick, I would still 
be anxious no matter which path you choose -- closing an HTTP connection 
takes some time due to TCP's 4-way close (FIN-ACK FIN-ACK) and you don't 
want your code from interfering.

On Thursday, February 21, 2013 1:06:13 PM UTC-5, Geerten van Meel wrote:
>
> The moment you use red.end() or res.render() data is being sent to the 
> client and the connection is closed afterwards. Whatever you do after that 
> point does not increase the time the request takes for the client*. Thus 
> all of your suggestions are fine and don't really make any difference. 
>
> *: Unless you perform blocking operations, like complex computing or 
> fs.*Sync shenanigans in the same tick. If you must, do that in the 
> following event iteration using process.nextTick or in a child process, 
> but, well, you should not block node anyways.
>
> The setTimeout approach though is both unnecessary and performs worse than 
> process.nextTick - don't use it unless you really need to wait a specific 
> time.
>
> On Thursday, February 21, 2013 7:08:56 AM UTC+1, benp...@tacol.biz wrote:
>>
>> Great Node.js!! I do love it.
>> Not sure if I can post question here. Apologized if I am wrong.
>>
>> I am wondering how can I finish the request and do something time 
>> consuming, e.g. email/file conversion...etc.
>> I've tried the following four ways, but all of them take long time to 
>> return the response in browser. 
>> Is there any way I can do something after the request is finished? Just 
>> like PHP FPM *fastcgi_finish_request()* in Express Project?
>>
>> #1 Use res.render callback
>>
>>     function(req, res){
>>
>>         res.render('someview', function(err, html){
>>
>>                 if (err){ console.log('debug-err:', err);
>>                 res.send(html);
>>                 var result = 1;
>>                 for (var i = 0, j = 10; i < 10000000000; i++){
>>                     result = result + i + j;
>>                 }
>>                 console.log('debug',result);
>>         });
>>     }
>>
>>
>> #2 Use setTimeout: 
>>  
>>     function(req, res){
>>
>>         res.render('someview');
>>
>>         setTimeout(function(){
>>                 var result = 1;
>>                 for (var i = 0, j = 10; i < 10000000000; i++){
>>                     result = result + i + j;
>>                 }
>>                 console.log('debug',result);
>>         }, 0);
>>     }
>>
>>
>> #3 Use process.nextTick:
>>
>>     function(req, res){
>>
>>         res.render('someview');
>>
>>         process.nextTick(function(){
>>                 var result = 1;
>>                 for (var i = 0, j = 10; i < 10000000000; i++){
>>                     result = result + i + j;
>>                 }
>>                 console.log('debug',result);
>>         });
>>     }
>>
>> #4 Simply do it, because res.render won't block the I/O
>>
>>     function(req, res){
>>
>>         res.render('someview');
>>         
>>         var result = 1;
>>         for (var i = 0, j = 10; i < 10000000000; i++){
>>             result = result + i + j;
>>         }
>>         console.log('debug',result);
>>     }
>>
>> Appreciated for any explanation for understanding Node.js event I/O. 
>> Thank you very much :)
>>
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to