如何在node.js中完成繁重的处理操作 [英] How to have heavy processing operations done in node.js

查看:50
本文介绍了如何在node.js中完成繁重的处理操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量的数据处理操作,每个10-12个模拟请求都需要完成.我已经读到,为了实现更高级别的并发,Node.js是一个很好的平台,它通过具有非阻塞事件循环来实现.

I have a heavy data processing operation that I need to get done per 10-12 simulatenous request. I have read that for higher level of concurrency Node.js is a good platform and it achieves it by having an non blocking event loop.

我知道的是,对于查询数据库这样的事情,我可以将一个事件派生到一个单独的进程中(例如 mongod mysqld ),然后创建一个回调将处理该过程的结果.足够公平.

What I know is that for having things like querying a database, I can spawn off an event to a separate process (like mongod, mysqld) and then have a callback which will handle the result from that process. Fair enough.

但是,如果我想在回调中进行大量计算,该怎么办?在该回调中的代码完全执行之前,它不会阻止其他请求.例如,我要处理高分辨率图像,而我拥有的代码是Javascript本身(没有单独的过程来进行图像处理).

But what if I want to have a heavy piece of computation to be done within a callback. Won't it block other request until the code in that callback is executed completely. For example I want to process an high resolution image and code I have is in Javascript itself (no separate process to do image processing).

我想到的实现方式就像

get_image_from_db(image_id, callback(imageBitMap) {
    heavy_operation(imageBitMap); // Can take 5 seconds.
});

在5秒钟内, heavy_operation 会阻止节点接受任何请求.还是我在思考执行此类任务的错误方法.请指导,我是JS新手.

Will that heavy_operation stop node from taking in any request for those 5 seconds. Or am I thinking the wrong way to do such task. Please guide, I am JS newbie.

更新

或者就像我可以处理部分图像并使事件循环返回以接受其他回调并返回到处理该部分图像一样.(类似于对事件进行优先级排序).

Or can it be like I could process partial image and make the event loop go back to take in other callbacks and return to processing that partial image. (something like prioritising events).

推荐答案

是的,它会阻止它,因为回调函数在主循环中执行.只有异步调用的函数不会阻塞循环.据我了解,如果您希望图像处理异步执行,则必须使用单独的进程来执行.

Yes it will block it, as the callback functions are executed in the main loop. It is only the asynchronously called functions which do not block the loop. It is my understanding that if you want the image processing to execute asynchronously, you will have to use a separate processes to do it.

请注意,您可以编写自己的异步过程来处理它.首先,您可以阅读如何为Node编写异步函数的答案.js .

Note that you can write your own asynchronous process to handle it. To start you could read the answers to How to write asynchronous functions for Node.js.

更新

如何创建一个node.js中的非阻塞异步功能?也许也值得一读.这个问题实际上是在我链接的另一个问题中提到的,但我想为了简单起见将其包含在这里.

how do i create a non-blocking asynchronous function in node.js? may also be worth reading. This question is actually referenced in the other one I linked, but I thought I'd include it here to for simplicity.

这篇关于如何在node.js中完成繁重的处理操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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