| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6522 人关注过本帖
标题:[转]Concurrency Models: Go vs Erlang
取消只看楼主 加入收藏
静夜思
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:济南的冬天
等 级:管理员
威 望:11
帖 子:8913
专家分:2567
注 册:2004-3-25
结帖率:100%
收藏
 问题点数:0 回复次数:1 
[转]Concurrency Models: Go vs Erlang
I like to think that I understand Go’s concurrency model pretty well. Last week, I learned Erlang (thanks to Learn You Some Erlang) and created my first Erlang application (a fully-asynchronous RPC implementation with Protobufs and zeromq). Despite the fundamental differences between the languages, Go being imperative and Erlang being impure functional, there are underlying concurrency models that appear very similar.
On the surface Go and Erlang have very similar concurrency models. Both allow easy spin up of “lightweight threads”, known as processes in Erlang and goroutines in Go. Essentially these are threads that are managed by the underlying runtime and not the OS. This layer of indirection provides significant value, as it allows the applications to spin up threads without worrying about the heavy weight (memory, swap, cpu, etc), as well as making it really easy.
In Go: go doSomething(). Erlang: spawn(fun do_something/0).
Both Go and Erlang provide message passing patterns. These are the core of concurrency, because there is no need to use shared memory (the root of all evil)! But they do it differently. Go provides channels, which can be thought of as in-memory sockets. Channels allow you to do many advanced patterns that I won’t get into here, so check them out if your interested. Channels have types and you can pass objects of those types through the channels between goroutines. Erlang processes have individual mailboxes which can receive messages of any type.
//Go
c := make(chan string, 1)
go func () { fmt.Println("Got %s from the channel", <- c) }()
c <- "foobar"
//Erlang
Pid = spawn(fun() ->
        receive Val -> io:format("Got %s from the mailbox", [Val])
        end
    end),
Pid ! "foobar".
Where Go and Erlang have the biggest differences is error handling. That probably wasn’t what you expected to read, but the two languages couldn’t have more different philosophies on errors. In Go code, you make your own errors and you handle them manually. The language (originally) has no way to immediately raise an error like practically every other language. Since then, they have added a panic function, but it is not meant to be used predominantly in production code. This is a common idiomatic pattern in Go:
result, err := doSomething()
if err != nil {
    // log it or return or ignore
}
Erlang is one of those exception languages. For all intents and purposes, it works similar to Java and Python. In Erlang, it is idiomatic to let your functions fail unless you specifically want to ignore errors. Here’s the three ways you can raise exceptions in Erlang.
throw(this_is_a_throw_exception),
error(this_is_an_error_exception),
exit(i_bet_you_can_guess_what_this_does).
In a vacuum, both schemes are valid, and I don’t really prefer either over the other. The issue comes up in concurrency. In go, the author handles all the errors manually, so he or she knows exactly which goroutines might fail and creates layers of indirection to handle them. Erlang, on the other hand allows, and even promotes as idiomatic, processes to exit due to error/throw/exit conditions. This creates some problems in long-term fail-safe programs. For instance, what if there are a pair of processes that work in unison and one of them dies?
Erlang manages this using a generic set of servers and supervisor implementations (known as the OTP). These generic implementations save the author from writing the boilerplate code of their long-running processes over an over again (and hence making mistakes). In writing a generic server process that can handle requests and hold state, you have to implement 7 different callbacks. Then you have to implement supervisors that monitor the worker processes to restart them as appropriate if they fail. These supervisors give the author a good measure of assurance that the process group will not die and stay dead, but it is still more complicated than a simple implementation.
In Go, you handle errors, and hence you manage everything about your goroutines and in-process servers. In Erlang, the language handles errors, and you have to manage the consequences of those errors. The Go implementation is more straightforward, while the Erlang implementation involves more callbacks and indirection.
In addition to the author’s experience, there are underlying differences in the concurrency implementations. Go is built to run on multicore processors and uses a shared-memory model. Erlang is built to run on multiple computers and does not allow shared-memory.
The concurrency models seem similar, but are in fact very different due to the error handling of each language. You can choose the one that fits best for your project.
Update: Since I wrote this article, I’ve had a lot of talks with some very knowledgeable Erlang programmers. My experience with Erlang is pretty minimal, and the opinions I’ve expressed were more from my experience creating my first Erlang programs, not the experience of 10 years working with idiomatic Erlang. That said, I should clear up a few things. There are a couple main other differences between the languages’ concurrency models:
    Erlang’s concurrency model uses “processes” and does not share memory, which Go’s goroutines do.
    Erlang’s scheduler is preemptive, which is a goal of Go’s but not the current state.
As for Erlang’s exception model, it is safer than Go’s because of OTP. The model is still more formal than Go’s, but OTP has made it safer because of monitor/restart semantics. From talking with some more knowlegeable Erlang people, its clear to me that error/exit/throw and try/catch syntax is rarely used, and the more appropriate “error as a value” is used idiomatically. This is the same as is forced in Go (since Go has no throw except for panic, but don’t use that). Thus perhaps the two models are not as dissimilar as I first suggested. The informality of the model in Go is still attractive, but the safety guarantees are better in Erlang.

本文转自:http://joneisen.me/post/38188396218
搜索更多相关主题的帖子: first understand between created similar 
2013-04-16 17:56
静夜思
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:济南的冬天
等 级:管理员
威 望:11
帖 子:8913
专家分:2567
注 册:2004-3-25
收藏
得分:0 
新开板块不发篇贴子无法建立缓存

畅所欲言
2013-04-16 17:57
快速回复:[转]Concurrency Models: Go vs Erlang
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017710 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved