在nodejs + express中结束请求后执行工作
Performing work after ending the request in nodejs + express
我想做一些客户端不需要知道的CPU处理。我正在考虑使用https://github.com/audreyt/node-webworker-threads包来做到这一点(例如,将音频文件从ogg转换为raw)。
我想象我可以使用res.send(200)
,然后像这样启动worker:
res.send(200);
var worker = new Worker(function(){
this.onmessage = function(event) {
//do conversion here by calling ffmpeg
};
});
worker.onmessage = function(event) {
//upload file to S3
};
worker.postMessage('filename');
此工作或表达式是否将等待此函数结束后发送回响应?这会阻塞事件线程吗?如果我正在使用webworker - threads,我希望不会。
您可以创建子进程来完成此操作。点击这个链接了解更多细节。要了解这个过程,请查看下面的代码。
const spawn = require('child_process').spawn;
app.post('/upload/audio', function (req, res) {
// Some Code here
// Code to run a child Process
var ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => { // This will run the process and keep receiving the logs as and when available (Async process)
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
// End the Child Process Block
// All the code above is async and will trigger the send way before any of your processes are completed
res.send("Success");
});
相关文章:
- 防止双击执行两次jQuery post请求
- 暂停函数执行流程,直到ajax请求完成
- Chrome浏览器“;挂起”;同时在循环中执行AJAX请求
- 如何仅在完成对gap.client.youtube的请求.execute后执行函数
- AJAX 请求不会在 400 个错误查询上执行失败
- 嵌套在 .get 请求中的 jQuery 不会执行
- 通过 ajax 请求执行 ajax 请求
- 如何在执行 Ajax 请求大量数据库更新时实现进度条
- 从 Web Worker 执行 AJAX 请求是否可行
- 在不执行ajax请求的情况下,使用jQuery读取HTTP标头以检查404
- Angular在不触发摘要的情况下执行XHR请求
- Django Ajax请求执行不正确
- 在 php 中为特定的 get 请求执行 javascript
- 跨域请求-执行所有脚本的页面内容
- Javascript/Jquery对象文字版本,对变量请求执行函数
- Ajax请求执行两次
- 为什么是护照?对每个请求执行serializeUser
- 从Ajax请求执行内联脚本
- 对不成功的jquery ajax请求执行一些操作
- Ajax请求执行失败,显示错误消息