我在计算密集型函数运行时轮询我的node / express应用程序。现在这个函数阻止node / express以某种方式立即回复到http-get请求;相反,这些轮询器请求排队并在计算完成后返回所有请求。
是否有一个模块或技术允许节点“呼吸”#34;并回复http请求?
在我的主要路线内
module.exports.parse = (req, res) ->
# Synchronously Cascading Waterfalls
async.waterfall [
(next) ->
MyOtherOtherModel.findById req.params.id, (err, data) ->
if err
next err
if !data._id
dataError = new Error "Fatal error: Data Result is empty!"
next dataError
next null, data
(data, next) ->
fs.readdir 'uploads', (err, files) ->
if err
next err
next null, data, files
(data, files, next) ->
async.each files, ((file, callback) ->
# Do intensive stuff here, parsing, I/O, mapping, etc
async.waterfall [
(innerNext) ->
MyOtherModel.findByIdAndUpdate data._id,
{ # data
$push:
'uploadedFiles':
fileName: file
length: data.length
},
{ # options
safe: true
upsert: true
new: true
}, (err, dataObjects) ->
if err
innerNext err
innerNext null, dataObjects
(dataObjects, innerNext) ->
async.each items, ((item, innerCallback) ->
MyModel.create myData, (err, result) ->
if err
innerCallback err
innerCallback null
), (err) ->
if err
innerNext err
innerNext null
], (err) ->
if err
callback err
callback null
), (err) ->
if err
next err
# Leave node room to breath, but it isnt working
setTimeout (->
next null
), 1000
], (err) ->
if err
throw Error err # All Errors bubble up to this point
# We return immediately, so that express is free to handle poller requests
res.status(202).send
status: 'OK'
error: 'Started Computation.'
return
我的投手
module.exports.poll = (req, res) ->
# Ask DB and calculate finishedPercentage
res.status(206).send
status : 'Partial Content'
type : 'info'
error : percentageFinished + ' % geparsed.'
progress : percentageFinished
控制台输出
Parsing..
nextFile..
nextFile..
nextFile..
nextFile..
Progress: 100.0 %
Progress: 100.0 %
...
因此,您可以看到轮询器以某种方式排队,直到Node认为它有时间回答http请求。我希望这一进展能够不断更新。
答案 0 :(得分:1)
一种解决方案是将CPU密集型任务委派给child process,该built-in message passing mechanism偶尔会通过节点{{3}}或仅通过写入stdout并让父进程中继该信息来报告进度更新回到客户端。