如何/为什么功能语言(特别是Erlang)扩展好? [英] How/why do functional languages (specifically Erlang) scale well?

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

问题描述

我一直在观察函数式编程语言和功能的可见性。我看着他们,没有看到上诉的理由。



然后,最近我在 Codemash



我喜欢这个演示文稿,并了解到函数式编程的许多属性使它更容易避免线程/并发问题。我知道缺乏状态和可变性使得多线程不可能改变相同的数据,但凯文说(如果我理解正确)所有的通信发生通过消息和消息同步处理(再次避免并发问题)。



但我已经阅读了Erlang用于高度可扩展的应用程序(爱立信首先创建它的全部原因)。如果一切都被作为同步处理的消息处理,那么如何能够每秒处理数千个请求?这不是为什么我们开始转向异步处理 - 所以我们可以利用同时运行多个线程的操作并实现可扩展性?看起来这个架构虽然更安全,但在可扩展性方面却倒退了一步。我缺少什么?



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


h2_lin>解决方案

函数式语言通常不会改变变量。因为这个,我们不必保护变量的共享状态,因为值是固定的。这反过来避免了传统语言在处理器或机器之间实施算法时大部分的跳跃。



Erlang比传统的函数式语言在消息传递系统中进行烘焙,该系统允许一切都在基于事件的系统上操作,其中一段代码仅担心接收消息和发送消息,而不担心更大的图片。



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



这样做的最终结果是每个代码段与每个其他代码段无关。没有共享代码,没有共享状态,以及来自a消息系统的所有交互都可以在许多硬件之间分发。



与传统系统对比:我们必须在protected变量和代码执行周围放置互斥体和信号量。我们在通过堆栈的函数调用中有紧密绑定(等待返回发生)。所有这些都会创建瓶颈,这在像Erlang这样的无共享系统中不是一个问题。



编辑:我还应该指出,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.

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

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

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?

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

How can functional programming languages be inherently thread-safe, yet still scale?

解决方案

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

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.

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

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

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

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