节点JS服务器停止侦听直到承诺(运行循环的async函数,希望使此循环在后台运行)已解决)
端口上的服务器代码
app.listen(PORT, () => {
console.log(`Server running on ${PORT}`);
});
是我正在收到请求的
app.get("/",(req, res) => {
res.header("Access-Control-Allow-Origin", "*");
console.log("before call");
callback(100,1000000000000).then(response=>console.log(response));
console.log("after call");
res.end();
});
这
async function callback(first, second) {
await null;
for (let i = first; i < second; i++) {
if (isPrime(i)) {
//writes to file if the number is prime
}
}
This is my server code which is listening on the port
app.listen(PORT, () => {
console.log(`Server running on ${PORT}`);
});
Here I am receiving a request and sending it to the function to process
app.get("/",(req, res) => {
res.header("Access-Control-Allow-Origin", "*");
console.log("before call");
callback(100,1000000000000).then(response=>console.log(response));
console.log("after call");
res.end();
});
This is the function that is processing incoming request
async function callback(first, second) {
await null;
for (let i = first; i < second; i++) {
if (isPrime(i)) {
//writes to file if the number is prime
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
node.js在一个线程上执行大多数操作,
主线程
,但是可以通过产卵子进程或使用
worker来使用多个线程。线程
。您可以阅读有关Worker线程
和子进程
的很多信息:文档:https://nodejs.org/api/worker_thread/worker_threads.html
https://nodejs.org/api/api/api/child/child_process.html_process.html#child-child-process
Worker线程
在像您这样的方案中特别有用,在您的情况下,您要执行繁重的同步任务,并且您不希望它们阻止Main Tread
。如果您具有异步I/ O的任务,例如文件读取/写作,它们具有同步和异步API等……最好使用内置的异步方法,因为它们是性能更高。Worker线程
,例如儿童进程
与主线程
通过Events
在消息的形式
和侦听器
。我在这里给您一个示例,这是一个简单的节点Web服务器,当它收到请求时,它将重生
Worker线程
并将其发送给带有两个nubmess的消息
和类型
。从现在开始,主线程保持免费,所有计算都在工作线程上执行。当Worker线程
已经完成了计算所有质数并将其写入文件时,它会通知主线程,主线程终止了工作。与您的情况的不同之处在于,在这种情况下,主线程不会被阻止,而其他客户请求可能会像往常一样通过处理:这是“ worker-thread.js”文件:
仅通过执行
app.js ,然后在Web浏览器上转到
127:0.0.1:5001
。您应该在半秒之内收到响应,但是工人将继续处理您的iSprime
循环,并将写入结果。Node.js executes most of the operations on one thread, the
main thread
, but it can use multiple threads, by spawningchild processes
, or by using theworker thread
. You can read a lot aboutworker thread
andchild processes
on docs:https://nodejs.org/api/worker_threads.html
https://nodejs.org/api/child_process.html#child-process
A
worker thread
is particularly useful in scenarios like yours, where you have heavy synchronous tasks to be executed and you don't want them to block themain thread
. If you have async I/O tasks like file reading/ writing, which have both sync and async API etc... it's better using the built-in async methods, since they are more performant.Worker threads
, likechild processes
communicate with themain thread
the usual node way, throughevents
that are emitted in the form ofmessages
, andlisteners
.I'll give you an example here, this is a simple Node web server, when it receives a request, it will respawn a
worker thread
and send it a message with twonumbers
and atype
. From now on the main thread remains free, and all the computation is performed on the worker thread. When theworker thread
has done to compute all prime numbers and write them in a file, it notifies the main thread, and the main thread terminates the worker. The difference with your case, is that in this case the main thread is not blocked, and other client requests may by processed as usual in the meanwhile:This is the "worker-thread.js" file:
Test it just by executing
App.js
and going to127:0.0.1:5001
on your web browser. You should receive the response within half a second, but the worker will keep processing yourisPrime
loop and will write to result.txt the result when done.