如何“共享状态” 与 Erlang 风格并发?
Erlang 使用并发模型来处理 Actor 之间的消息传递。
假设我有 3 名演员出售物品。 商品总数为 7 件。他们如何准确地卖出 7 件商品? 他们如何协调自己? 我们可以让一个角色负责处理“购买”消息(库存角色),并掌握可用物品的数量。 但这将是一个SPOF。
当使用消息队列而不是锁来实现并发时,其他语言(例如 Java)的并发性也是如此。
(最好没有 Amdahl 瓶颈)
Erlang works with message passing between actors as it's concurrency model.
Assume I have 3 actors who sell items. The total number of items is 7. How do they excactly sell 7 items? How do they coordinate themselves? We could have one actor with the number of available items, acting on "buy" messages (inventory actor). This would be a SPOF though.
The same goes concurrency in other languages like Java when using message queues for concurrency instead of locks.
(Best without an Amdahl bottleneck)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有一个代表事物的流程 - 在本例中为库存。 其他流程要买的时候就问库存,你有吗? 我可以买吗?
代表交货的流程将告诉库存,这里有 20 个新事物......
You have a process that represents a thing - in this case an inventory. When other processes want to buy, they ask the inventory, do you have one? can I buy it?
A process representing a delivery will tell the inventory, here are 20 new things...
我将实现一个负责库存管理的服务器进程,例如使用 dets 或 Mnesia 表作为后端。 该进程可以是监督树的一部分,因此如果失败,它将自动重新启动。 我的推销员进程(您上面提到的 3 个参与者)与此服务器进程进行通信,询问其销售的商品。 只要有足够的商品就没有问题,否则销售人员会得到商品已售完的答案,而另一个流程会获取购买新商品的信息。
I would implement a server process responsible for the stock management, e.g. using a dets or a Mnesia table as backend. This process could be part of a supervision tree, so if it fails it would be restarted automatically. My salesman processes - the 3 actors you've mentioned above - communicate with this server process asking it for the items they sell. As long as there are enough items there's no problem, otherwise the salesmen would get the answer that the item is sold out - and another process gets the information to purchase new ones.
这些演员是在真空中表演的吗? 他们必须有 7 个买家或 7 个库存商品。 也许排队的买家或库存商店应该协调他们。
Do these actors act in a vacuum? They must either have 7 buyers or 7 items in inventory. Perhaps the queued buyers or inventory store should coordinate them.