node.js中的“生成线程"行为 [英] "Spawn a thread"-like behaviour in node.js

查看:88
本文介绍了node.js中的“生成线程"行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些管理实用程序添加到一个小的Web应用程序中,例如备份数据库".用户将单击一个按钮,并且HTTP响应将立即返回,尽管潜在的长期运行过程已在后台启动.

I want to add some admin utilities to a little Web app, such as "Backup Database". The user will click on a button and the HTTP response will return immediately, although the potentially long-running process has been started in the background.

在Java中,这可能是通过使用Actor在Scala中生成独立线程来实现的.但是,node.js中合适的惯用法是什么? (赞赏代码段)

In Java this would probably be implemented by spawning an independent thread, in Scala by using an Actor. But what's an appropriate idiom in node.js? (code snippet appreciated)

我现在正在重新阅读文档,这确实确实是一个节点101的问题,但这几乎是我在此上的位置...无论如何,要澄清这是基本情况:

I'm now re-reading the docs, this really does seem a node 101 question but that's pretty much where I am on this...anyhow, to clarify this is the basic scenario :

function onRequest(request, response) {
    doSomething();
    response.writeHead(202, headers);
    response.end("doing something");
}

function doSomething(){
     // long-running operation
}

我希望响应立即返回,而doSomething()在后台运行.

I want the response to return immediately, leaving doSomething() running in the background.

好吧,考虑到节点的单线程模型,如果不生成另一个OS级的ChildProcess似乎是不可能的.我的误会

Ok, given the single-thread model of node that doesn't seem possible without spawning another OS-level ChildProcess. My misunderstanding.

在我的代码中,备份所需的大部分内容都是基于I/O的,因此节点应以一种不错的异步方式进行处理.我想我要做的就是将doSomething移到response.end之后,看看它的行为.

In my code what I need for backup is mostly I/O based, so node should handle that in a nice async fashion. What I think I'll do is shift the doSomething to after the response.end, see how that behaves.

推荐答案

我没有看到问题.您需要做的只是让doSomething()启动异步操作.它会立即返回,您的onRequest将写回响应,并且客户端将收到他们的确定,我开始"的消息.

I don't see the problem. All you need to do is have doSomething() start an asynchronous operation. It'll return immediately, your onRequest will write the response back, and the client will get their "OK, I started" message.

function doSomething() {
    openDatabaseConnection(connectionString, function(conn) {
        // This is called some time later, once the connection is established.
        // Now you can tell the database to back itself up.
    });
}

doSomething不会只是坐在那里,直到建立数据库连接,或者等待您告诉它进行备份.它会立即返回,并注册了一个稍后将运行的回调.在后台,数据库库可能正在为您创建一些线程,以使异步按应有的方式工作,但是您的代码不必担心它;您只需立即返回,立即将响应发送给客户端,异步代码就会保持异步运行.

doSomething won't just sit there until the database connection is established, or wait while you tell it to back up. It'll return right away, having registered a callback that will run later. Behind the scenes, your database library is probably creating some threads for you, to make the async work the way it should, but your code doesn't need to worry about it; you just return right away, send the response to the client right away, and the asynchronous code keeps running asynchronously.

(实际上,要使其同步运行还需要做更多工作-您必须将response对象传递到doSomething中,并在备份完成后,在最里面的回调函数中让doSomething进行response.end调用当然,这不是您要执行的操作;您想立即返回,这正是您的代码将执行的操作.)

(It's actually more work to make this run synchronously -- you would have to pass your response object into doSomething, and have doSomething do the response.end call inside the innermost callback, after the backup is done. Of course, that's not what you want to do here; you want to return immediately, which is exactly what your code will do.)

这篇关于node.js中的“生成线程"行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆