Concurrency is easy -- 并行计算轻松理解版 -- swordsp 07-01-29 发现并推荐

::-- ZoomQuiet [2007-01-30 06:25:45]

CPUG联盟::

CPUG::门户plone

BPUG

SPUG

ZPUG

SpreadPython Python宣传

1. 试译

http://photos1.blogger.com/blogger2/3098/640384196112295/320/many_cars.0.jpg

1.1. Concurrency is easy

我们本来就理解并发的.

大脑其实已经硬件植入了并发机制的.

我们对刺激响应极其的快捷,大脑有专门一部分进行对淋巴系统的调用,没有这,我们早死了, 而意识的反应就比较慢了,例如在我们意识到"要刹车!"之时,但是身体其实已经作了.

在高速公路,我们同时要跟踪几十种情况,或是上百辆车,这都没有经过意识的处理, 如果不是这样,我们可能已经死了.

We understand concurrency

A deep understanding of concurrency is hard-wired into our brains. We react to stimulation extremely quickly, in a part of the brain called the amygdala, without this reaction system we would die. Conscious thought is just too slow, by the time the thought "hit the brakes" has formed itself, we have already done it.

On a motorway, I mentally track the positions of dozens, or hundreds of cars, this is done without conscious thought. If I couldn't do this I would probably be dead.

1.1.1. 世界是并行的

The world is parallel

如果我们想在现实世界中完成一种行为同其它对象一般规矩 的程序,它必然是并行的.

If we wish to write programs that behave as other objects behave in the real world then these programs will have a concurrent structure.

1.1.2. 这就是为什么我们要使用并发语言进行编程

This is why we should program in a concurrent programming language

当然我们在现实世界中最常见的是使用序列语言进行编程,想解放思想很难. ErLang 是被设计为天然进行并发处理的语言,使用它来编写并发程序非常简单.

And yet most often we program real-world applications in sequential programming languages. This is uncessarily difficult.

By programming in a language what was designed for programming concurrent applications (like Erlang) concurrent programming becomes a lot easier.

1.1.3. Erlang 程序模仿我们的思想和交互

Erlang programs model how we think and interact

我们没有共享记忆,各有各的,我们共有两个大脑,它们并没有合并在一起. 为了改变你的记忆,我得发出消息,说话或是挥动手臂. 你听到,看到,你的记忆就变更了,但是不问一下你或是不观察你的反应,我没有办法确认你接受了我的消息.

这就是Erlang 的处理过程, ErLang的进程没有分享内存,每个进程各用自个儿的. 改变另外进程,你必须给其发送渻,并期望它可以接受并理解.

为确认另外进程接受你的消息并变更了它的内存,你必须查询(通过消息的发送). 这恰恰是我们如何交互的过程.

We don't have shared memory. I have my memory, you have yours, we have two brains, one each, they are not joined together. To change your memory I send you a message, I talk or wave my arms. You listen, you see, your memory changes, but without asking you a question or observing your response I do not know that you have received my messages.

This is how it is with Erlang processes. Erlang processes have no shared memory. Each processes has its own memory. To change the memory of some other process you must send it a message and hope that it receives and understands the message.

To confirm that another process has received your message and changed its memory, you must ask it (by sending it a message). This is exactly how we interact.

李四:"嘿,张三我电话是 12345678 "

李四:"听到? "

张三:"是,你电话是 12345678"

Sue: "Hi Bill, my telephone number is 45 67 89 12"

Sue: "Did you hear me?"

Bill: "sure, your number is 45 67 89 12"

这种交互模型,我们都明白,一出生,我们就一直在学习如何通过发送消息和观察反应来同世界交互.

These interaction patterns well-know to us, from birth onwards we learn to interact with the world by observing it and by sending it messages and observing the responses.

1.1.4. 人们作为独立应用运行使用消息的发送进行沟通

People function as independent entities that communicate by sending messages

ErLang正如我们一样进行处理的,所以理解Erlang 程序是非常容易的.

ErLang 程序生成N多小进程自由运行 -- 就象人群.

每个ErLang 可以制造几十, 可能上千的小进程 -- 所有进程的运行是独立的. 它们相互沟通就是使用消息的发送. 每个进程都有私有的内存. 如同一个大空间中的人群相互游离.

That's how Erlang processes work, and that's how we work so it's very easy to understand an Erlang program.

Erlang programs are made up of lots of little processes all chattering away to each other - just like people.

An Erlang program is made up of dozens, possible thousands or even hundreds of thousands of small processes - all these processes operate independently. They communicate with each other by sending messages. Each process has a private memory. They behave like a huge room of people all chattering away to each other.

这使得ErLang 程序天生的容易管理及衡量. 假定我们有10人(进程)而且都很忙,我们如果处理的? 如何管理人群,最简单的这是发出指令给所有人(广播).

ErLang 程序没有分享内存,所以不必进行锁操作--进行协同时, 否则: 锁在哪?钥匙在哪?如果哪天钥匙丢了会发生什么?... 你就得恐慌了.

分布式系统使用锁机制的话,最终总会错乱的.

This makes Erlang program inherently easy to manage and scale. Suppose we have ten people (processes) and they have too much work to do, what can we do? get more people. How can we manage these groups of people, easy just shout instructions at them (broadcasting).

Erlang processes don't have shared memory, so there is no need to lock the memory while it is being used. Where there are locks, there are keys, and when there are keys the keys will one day get lost and what happens when you loose a key? - you panic.

Distributed software systems with locks and keys always go wrong.

1.1.5. 如果有人死了人们应该注意到

If somebody dies other people will notice

如果要在房间里突然昏倒死了,应该有人将注意到,至少最终应该 -- 我希望. ErLang 进程也正如人群,它们有时也会死. 不象人,他们会精确的在死亡之时咽下最后一口气.

相象间塞满了人的房间,突然有人要倒下去死时,他会说:"我心脏要完了!"或是"我胃穿孔了,我要死了!"

ErLang 进程也正是这么作的,一个进程可以这么说:"我要求除以0,我要完了!" 另一个可能说:"我要完了,因为我向一空列表请求最后的元素!"

I I'm in a room and suddenly keel over and die, somebody will probably notice, well at least I hope so. Erlang processes are just like people, they can on occasions die. Unlike people when they die they shout out in their last breath exactly what they have died from.

Imagine a room full of people, suddenly one person will keel over and die and just as they die, they say "I'm dying of a heart attack" or "I'm dying of an exploded gastric wobbledgog". That's what Erlang processes do. One process might die saying.

"I'm dying because I was asked to divide by zero", another might say

"I'm dying because because I was asked what the last element in an empty list was"

现在我们可以想象那塞满了人的房间里,必然有专门的人来清理尸体. 让我们想象两个人 甲同乙, 如果甲死亡后由乙来处理关联的问题. 乙死亡后由甲来处理关联的问题, 甲和乙就象是被看不见的合约绑定在了一起 -- 不论谁死,都得由对方善后(好哥们儿哪;-)

Now in our room full of people we might imagine there are specially assigned people whose job it is to clear away the bodies. Let's imagine two people Jane and John. If Jane dies then John will fix any problems associated with Jane's death. If Jane dies then John will fix the problems. Jane and John are linked together with an invisible agreement which says that if one of them dies the other will fix up any problems caused by the death.

这就是ErLang 程序如何察觉错误的,进程,可以联接在一起.如果其中之一死亡,则其它进程获得消息说明此为什么死亡了.

这是基本行为,这就是ErLang 程序的工作方式.

That's is how error detection in Erlang works, processes can be linked together. If one of the processes dies, the other process gets an error message saying why the first process dies.

That's basically it.

That's how Erlang programs work.

1.1.6. 小结

What we've learnt so far

ErLang 程序生成大量进程,进程能相互发送消息. 消息可能被接受并理解,也可能不.

如果你要确认消息是否被接受,你要主动发送查詢消息并等待回应.

一对进程可以相互联接.如果其中之一死亡,将发送给联接的另一方消息,说明死亡原因.

Erlang programs are made of lots of processes. These processes can send messages to each other.

These message may or may not be received and understood. If you want to know if a message was received and understood you must send the process a message and wait for a reply.

Pairs of processes can be linked together. If one processes in a linked pair dies the other process in the pair will be sent a message containing the reason why the first process died.

这便是 面向并发程序 的简单模式说明. 进一步的可以阅读这里

This simple model of programming is part of a model I call Concurrency Oriented Programming. You can read more about this here.

2. 反馈

:),0
HuangYi
2007-01-31 15:20:14

The world is parallel 
的意思应该是: 
世界是并行的
吧。

:\,0
ZoomQuiet
2007-02-01 00:11:45

good! fixed it!

ErLang/ConcurrencyEasy (last edited 2009-12-25 07:15:53 by localhost)