我在node js中有一段代码需要几秒钟才能完成,所以我开始使用threads_a_gogo模块(https://github.com/xk/node-threads-a-gogo)来不阻止http请求。我花了很多时间试图找出为什么威胁生成不起作用,因为我的请求仍然被阻止。
app.post('/results.html', function(req, res){ if( config.debug ) console.log("thread created"); var t = threads.create(); var r = new rounds(); t.eval( r.start(req,res), function(err, result) { r.finish( res ); //t.destroy(); if( config.debug ) console.log("thread destroyed"); }); console.log("request finished"); });
console.log("请求完成");仅在新线程完成后显示,但它应该几乎在请求完成时显示,并且在进程完成之前阻止对服务器的请求。
知道我做错了什么吗?
谢谢
最佳答案
您实际上是在主线程中执行r.start(req,res)
,然后将返回值传递给t.eval
。 eval
接受一个字符串,然后在线程全局范围内执行。像这样的东西应该有效:
t.eval('var start =' + r.start.toString()).eval('start()'), function(err, result) { });
关于multithreading - node.js +express +threads_a_gogo 阻塞请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20400256/