执行前preSS res.render以异步方式 [英] Executing Express res.render in a async manner

查看:128
本文介绍了执行前preSS res.render以异步方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有其中 res.render 前preSS的方法正在约400毫秒的阻塞方式应用的NodeJS。如何处理这在非阻塞的方式执行?我的Apache基准需要12秒左右执行30个并发请求。我如何以更好的方式实现这一点?

I have a nodejs application where res.render method of express is taking about 400 ms in a blocking way. How do I handle this to execute in a non blocking way? My apache benchmark takes 12 seconds for executing around 30 concurrent requests. How do I implement this in a better manner?

var start = +new Date;
//fetch data from redis
console.log('time taken to fetch data from redis ' + (+new Date - start)); //30 ms
res.render('some_jade_view', params);
console.log('time taken to render data ' + (+new Date - start)); //530 ms

我试过 process.nextTick ,但它并没有太大的帮助,AB结果都是一样的。

I tried process.nextTick but it did not help much, ab results are the same.

推荐答案

我觉得你真的应该看看到 HTTPS ://github.com/caolan/async

I think you should really take a look into https://github.com/caolan/async.

直接从异步的回购:

异步是一种实用工具模块,它提供了直接的,功能强大
  功能异步JavaScript的工作。 (...)

Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. (...)

异步提供大约20个功能,包括通常的'功能'
  嫌疑人(地图,减少,滤波器,每个...)以及一些共同的模式
  异步控制流(并联,串联,瀑布...)。所有
  这些功能假定您按照提供的node.js的约定
  一个回调作为您的异步函数的最后一个参数。

Async provides around 20 functions that include the usual 'functional' suspects (map, reduce, filter, each…) as well as some common patterns for asynchronous control flow (parallel, series, waterfall…). All these functions assume you follow the node.js convention of providing a single callback as the last argument of your async function.

干杯。

编辑:我不知道,如果渲染以异步方式您认为将真正帮助你减少你的时间。您可能希望实现上取出并因为它是磨磨蹭蹭模板中的数据在客户端的数据流。你可以使用像该角,或做手工。

I'm not sure if rendering your view in an asynchronous manner would really help you reduce your times. You may want to implement a stream on the client side that fetches and templates the data as it is coming along. You could use a front end framework like Angular for that, or do it manually.

这篇关于执行前preSS res.render以异步方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆