函数式语言(特别是 Erlang)如何/为什么可以很好地扩展? [英] How/why do functional languages (specifically Erlang) scale well?

查看:26
本文介绍了函数式语言(特别是 Erlang)如何/为什么可以很好地扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在关注函数式编程语言和功能日益增长的知名度.我调查了他们,没有看到上诉的原因.

I have been watching the growing visibility of functional programming languages and features for a while. I looked into them and didn't see the reason for the appeal.

然后,最近我在 Codemash 参加了 Kevin Smith 的Erlang 基础"演讲.

Then, recently I attended Kevin Smith's "Basics of Erlang" presentation at Codemash.

我很喜欢这次演讲,并了解到函数式编程的许多特性使避免线程/并发问题变得更加容易.我理解缺乏状态和可变性使得多个线程无法更改相同的数据,但是 Kevin 说(如果我理解正确的话)所有通信都是通过消息进行的,并且消息是同步处理的(再次避免并发问题).

I enjoyed the presentation and learned that a lot of the attributes of functional programming make it much easier to avoid threading/concurrency issues. I understand the lack of state and mutability makes it impossible for multiple threads to alter the same data, but Kevin said (if I understood correctly) all communication takes place through messages and the mesages are processed synchronously (again avoiding concurrency issues).

但我读到 Erlang 用于高度可扩展的应用程序(爱立信最初创建它的全部原因).如果所有内容都作为同步处理的消息处理,那么如何高效地处理每秒数千个请求?这难道不是我们开始转向异步处理的原因——这样我们就可以利用同时运行多个操作线程并实现可伸缩性吗?看起来这种架构虽然更安全,但在可扩展性方面却是倒退了一步.我错过了什么?

But I have read that Erlang is used in highly scalable applications (the whole reason Ericsson created it in the first place). How can it be efficient handling thousands of requests per second if everything is handled as a synchronously processed message? Isn't that why we started moving towards asynchronous processing - so we can take advantage of running multiple threads of operation at the same time and achieve scalability? It seems like this architecture, while safer, is a step backwards in terms of scalability. What am I missing?

我理解 Erlang 的创建者故意避免支持线程以避免并发问题,但我认为多线程是实现可扩展性所必需的.

I understand the creators of Erlang intentionally avoided supporting threading to avoid concurrency problems, but I thought multi-threading was necessary to achieve scalability.

函数式编程语言如何在本质上是线程安全的,但仍然可以扩展?

推荐答案

函数式语言(通常)不依赖于 变异 一个变量.因此,我们不必保护变量的共享状态",因为值是固定的.这反过来又避免了传统语言在跨处理器或机器实现算法时必须经历的大部分跳跃.

A functional language doesn't (in general) rely on mutating a variable. Because of this, we don't have to protect the "shared state" of a variable, because the value is fixed. This in turn avoids the majority of the hoop jumping that traditional languages have to go through to implement an algorithm across processors or machines.

Erlang 比传统的函数式语言更进一步,通过在消息传递系统中进行烘焙,该系统允许一切都在基于事件的系统上运行,其中一段代码只关心接收消息和发送消息,而不关心更大的图景.

Erlang takes it further than traditional functional languages by baking in a message passing system that allows everything to operate on an event based system where a piece of code only worries about receiving messages and sending messages, not worrying about a bigger picture.

这意味着程序员(名义上)不关心消息将在另一个处理器或机器上处理:简单地发送消息就足以让它继续.如果它关心响应,它将作为另一条消息等待它.

What this means is that the programmer is (nominally) unconcerned that the message will be handled on another processor or machine: simply sending the message is good enough for it to continue. If it cares about a response, it will wait for it as another message.

这样做的最终结果是每个片段都独立于其他每个片段.没有共享代码,没有共享状态,所有交互都来自一个可以分布在许多硬件(或不分布)之间的消息系统.

The end result of this is that each snippet is independent of every other snippet. No shared code, no shared state and all interactions coming from a a message system that can be distributed among many pieces of hardware (or not).

将此与传统系统进行对比:我们必须在受保护的"变量和代码执行周围放置互斥体和信号量.我们通过堆栈(等待返回发生)在函数调用中进行了紧密绑定.所有这些都造成了瓶颈,而这些瓶颈在 Erlang 这样的无共享系统中并不是什么问题.

Contrast this with a traditional system: we have to place mutexes and semaphores around "protected" variables and code execution. We have tight binding in a function call via the stack (waiting for the return to occur). All of this creates bottlenecks that are less of a problem in a shared nothing system like Erlang.

我还应该指出 Erlang 是异步的.你发送你的消息,也许/有一天另一条消息回来了.或不.

I should also point out that Erlang is asynchronous. You send your message and maybe/someday another message arrives back. Or not.

Spencer 关于乱序执行的观点也很重要并且得到了很好的回答.

Spencer's point about out of order execution is also important and well answered.

这篇关于函数式语言(特别是 Erlang)如何/为什么可以很好地扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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