【发布时间】:2015-11-20 01:13:39
【问题描述】:
所以我需要一种方法,让node计算这个我希望它计算的巨大的东西并立即返回202 Accepted,并且计算的状态是通过Poller询问的。有什么想法吗?
我的 Node/Express Endpoints 中已经有这样的场景:
module.export.poller = (req,res) ->
res.status(200).send({
percentFinished: precentFinished
})
module.export.beginHugeCalculation = (req,res) ->
async.waterfall [
(next) ->
# very, very, very, very time-intensive calculation here
next null
], (err) ->
# Success
return
# Immediately return HTTP 202 after Async Waterfall is triggered!
# Status can be requested via Poller Route
res.status(202).send({
inProgress : true
})
return
这在后端的单元测试中按预期工作,触发异步,然后返回 202,通过 Poller API 我可以询问进度(在其他地方计算)。关键是,一旦我在手动测试中从前端执行此操作,beginHugeCaluclation 就会被触发并返回 202,并且我每秒发送的轮询器请求会排队并仅在异步完成时立即返回,返回100% 在每个请求中。
【问题讨论】:
-
对我来说,这听起来像是一个 同步 任务正在阻塞你的线程。 (支持@GiladBison 的回答)。在计算完成之前,某处的回调中的
return语句无法发生。