“Spawn a thread”-like behaviour in node.js

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.

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)

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
}

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

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

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.

Problem courtesy of: danja

Solution

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
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.

(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.)

Solution courtesy of: Joe White

稿源:Node.js Recipes (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 后端存储 » “Spawn a thread”-like behaviour in node.js

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录