Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've started tinkering with Node.js HTTP server and really like to write server side Javascript but something is keeping me from starting to use Node.js for my web application.(我已经开始修补Node.js HTTP服务器,并且非常喜欢编写服务器端Javascript,但有些东西阻止我开始使用Node.js作为我的Web应用程序。)

I understand the whole async I/O concept but I'm somewhat concerned about the edge cases where procedural code is very CPU intensive such as image manipulation or sorting large data sets.(我理解整个异步I / O概念,但我有点担心程序代码非常CPU密集的边缘情况,例如图像处理或排序大数据集。) As I understand it, the server will be very fast for simple web page requests such as viewing a listing of users or viewing a blog post.(据我了解,服务器对于简单的网页请求非常快,例如查看用户列表或查看博客帖子。) However, if I want to write very CPU intensive code (in the admin back end for example) that generates graphics or resizes thousands of images, the request will be very slow (a few seconds).(但是,如果我想编写非常CPU密集型代码(例如在管理员后端)生成图形或调整数千个图像的大小,请求将非常慢(几秒钟)。) Since this code is not async, every requests coming to the server during those few seconds will be blocked until my slow request is done.(由于此代码不是异步的,因此在几秒钟内发送到服务器的每个请求都将被阻止,直到我的慢请求完成为止。) One suggestion was to use Web Workers for CPU intensive tasks.(一个建议是使用Web Workers进行CPU密集型任务。) However, I'm afraid web workers will make it hard to write clean code since it works by including a separate JS file.(但是,我担心网络工作者会很难编写干净的代码,因为它的工作方式是包含一个单独的JS文件。) What if the CPU intensive code is located in an object's method?(如果CPU密集型代码位于对象的方法中该怎么办?) It kind of sucks to write a JS file for every method that is CPU intensive.(为每个CPU密集型方法编写一个JS文件真是太糟糕了。) Another suggestion was to spawn a child process, but that makes the code even less maintainable.(另一个建议是生成子进程,但这使得代码更难以维护。) Any suggestions to overcome this (perceived) obstacle?(有什么建议可以克服这个(感知的)障碍吗?) How do you write clean object oriented code with Node.js while making sure CPU heavy tasks are executed async?(如何使用Node.js编写干净的面向对象代码,同时确保CPU重任务执行异步?)   ask by Olivier Lalonde translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
226 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is misunderstanding of the definition of web server -- it should only be used to "talk" with clients.(这是对Web服务器定义的误解 - 它应该只用于与客户“交谈”。)

Heavy load tasks should be delegated to standalone programs (that of course can be also written in JS).(重载任务应该委托给独立程序(当然也可以用JS编写)。)
You'd probably say that it is dirty, but I assure you that a web server process stuck in resizing images is just worse (even for lets say Apache, when it does not block other queries).(您可能会说它很脏,但我向您保证,调整图像大小的Web服务器进程更糟糕(即使对于Apache来说,当它不阻止其他查询时)。) Still, you may use a common library to avoid code redundancy.(不过,您可以使用公共库来避免代码冗余。) EDIT: I have come up with an analogy;(编辑:我想出了一个类比;) web application should be as a restaurant.(网络应用程序应该作为一个餐厅。) You have waiters (web server) and cooks (workers).(你有服务员(网络服务员)和厨师(工人)。) Waiters are in contact with clients and do simple tasks like providing menu or explaining if some dish is vegetarian.(服务员与客户保持联系并完成简单的任务,例如提供菜单或解释某些菜是素食的。) On the other hand they delegate harder tasks to the kitchen.(另一方面,他们将更艰巨的任务委托给厨房。) Because waiters are doing only simple things they respond quick, and cooks can concentrate on their job.(因为服务员只做简单的事情,他们反应迅速,厨师可以集中精力完成工作。) Node.js here would be a single but very talented waiter that can process many requests at a time, and Apache would be a gang of dumb waiters that just process one request each.(这里的Node.js将是一个单一但非常有才华的服务员,可以一次处理多个请求,而Apache将是一群愚蠢的服务员,每个服务器只处理一个请求。) If this one Node.js waiter would begin to cook, it would be an immediate catastrophe.(如果这个Node.js服务员开始做饭,那将是一场直接的灾难。) Still, cooking could also exhaust even a large supply of Apache waiters, not mentioning the chaos in the kitchen and the progressive decrease of responsitivity.(尽管如此,烹饪也可能耗尽大量的Apache服务员,没有提到厨房里的混乱以及响应能力的逐渐下降。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...