Haskell对Node.js的响应是什么? [英] What is the Haskell response to Node.js?

查看:133
本文介绍了Haskell对Node.js的响应是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信Erlang社区不羡慕Node.js,因为它本身就是非阻塞I / O,并且可以轻松地将部署扩展到多个处理器(甚至内置在Node.js中)。有关详情,请访问 http://journal.dedasys.com/2010/04/29/erlang -vs-node-js Node.js或Erlang

I believe the Erlang community is not envious of Node.js as it does non-blocking I/O natively and has ways to scale deployments easily to more than one processor (something not even built-in in Node.js). More details at http://journal.dedasys.com/2010/04/29/erlang-vs-node-js and Node.js or Erlang

Haskell怎么样? Haskell可以提供Node.js的一些好处,即一个干净的解决方案,以避免阻塞I / O而不求助于多线程编程?

What about Haskell? Can Haskell provide some of the benefits of Node.js, namely a clean solution to avoid blocking I/O without having recourse to multi-thread programming?

UPDATE:

有很多东西对Node.js很有吸引力

There are many things that are attractive with Node.js


  1. 操作,程序员只提供回调(如Snap框架中)

  2. 回调可以保证在单个线程中运行:没有竞争条件。

  3. 尼斯和简单的UNIX友好的API。奖金:优秀的HTTP支持。 DNS也可用。

  4. 每个I / O都默认为异步。这使得更容易避免锁。但是,回调中的太多CPU处理会影响其他连接(在这种情况下,任务应该拆分为更小的子任务并重新调度)。

  5. 客户端使用相同的语言和服务器端。 (我没有看到太多的价值在这一个,然而,jQuery和Node.js共享事件编程模型,但其余是非常不同的我只是看不到在服务器端和客户端之间的共享代码可以

  1. Events: No thread manipulation, the programmer only provides callbacks (as in Snap framework)
  2. Callbacks are guaranteed to be run in a single thread: no race condition possible.
  3. Nice and simple UNIX-friendly API. Bonus: Excellent HTTP support. DNS also available.
  4. Every I/O is by default asynchronous. This makes it easier to avoid locks. However, too much CPU processing in a callback will impact other connections (in this case, the task should split into smaller sub-tasks and re-scheduled).
  5. Same language for client-side and server-side. (I don't see too much value in this one, however. jQuery and Node.js share the event programming model but the rest is very different. I just can't see how sharing code between server-side and client-side could be useful in practice.)
  6. All this packaged in a single product.


推荐答案

好的,所以观看了一些 node.js演示 @gawi指向我,我可以说一点关于Haskell如何与node.js比较。在演示中,Ryan描述了Green Threads的一些好处,但接下来他说他没有发现缺少线程抽象是一个缺点。我不同意他的立场,特别是在Haskell的上下文中:我认为线程提供的抽象是使服务器代码更容易得到正确,更健壮的关键。特别是:

Ok, so having watched a little of the node.js presentation that @gawi pointed me at, I can say a bit more about how Haskell compares to node.js. In the presentation, Ryan describes some of the benefits of Green Threads, but then goes on to say that he doesn't find the lack of a thread abstraction to be a disadvantage. I'd disagree with his position, particularly in the context of Haskell: I think the abstractions that threads provide are essential for making server code easier to get right, and more robust. In particular:


  • 每个连接使用一个线程,您可以编写代码来表达与单个客户端的通信,同时处理所有客户端。想象它这样:处理多个客户端线程的服务器看起来几乎一样处理单个客户端;主要区别是前者有一个 fork 。如果你正在实现的协议是复杂的,同时为多个客户端管理状态机变得很棘手,而线程允许你只是脚本与单个客户端的通信。

  • using one thread per connection lets you write code that expresses the communication with a single client, rather that writing code that deals with all the clients at the same time. Think of it like this: a server that handles multiple clients with threads looks almost the same as one that handles a single client; the main difference is there's a fork somewhere in the former. If the protocol you're implementing is at all complex, managing the state machine for multiple clients simultaneously gets quite tricky, whereas threads let you just script the communication with a single client. The code is easier to get right, and easier to understand and maintain.

单个操作系统线程上的回调是协作多任务处理,而不是抢占式多任务处理是你得到的线程。协作多任务的主要缺点是程序员负责确保没有饥饿。它失去模块性:在一个地方犯错误,它可以拧整个系统。这真的是你不想担心的,抢占是一个简单的解决方案。

callbacks on a single OS thread is cooperative multitasking, as opposed to preemptive multitasking, which is what you get with threads. The main disadvantage with cooperative multitasking is that the programmer is responsible for making sure that there's no starvation. It loses modularity: make a mistake in one place, and it can screw up the whole system. This is really something you don't want to have to worry about, and preemption is the simple solution. Moreover, communication between callbacks isn't possible (it would deadlock).

并发在Haskell中并不困难,因为大多数代码都是纯的,线程也是如此 - 安全施工。有简单的通信原语。

concurrency isn't hard in Haskell, because most code is pure and so is thread-safe by construction. There are simple communication primitives. It's much harder to shoot yourself in the foot with concurrency in Haskell than in a language with unrestricted side effects.

这篇关于Haskell对Node.js的响应是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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