简体   繁体   English

Erlang中的进程平衡

[英]Process balancing in Erlang

Does anybody knows if there is a sort of 'load-balancer' in the erlang standard library? 有没有人知道erlang标准库中是否存在某种“负载均衡器”? I mean, if I have some really simple operations on a really large set of data, the overhead of constructing a process for every item will be larger than perform the operation sequentially. 我的意思是,如果我对一组非常大的数据进行一些非常简单的操作,那么为每个项目构建一个进程的开销将大于按顺序执行操作。 But if I can balance the work in the 'right number' of process, it will perform better, so I'm basically asking if there is an easy way to accomplish this task. 但是,如果我可以在“正确数量”的过程中平衡工作,它会表现得更好,所以我基本上都在询问是否有一种简单的方法来完成这项任务。

By the way, does anybody knows if an OTP application does some kind of balance load? 顺便问一下,有人知道OTP应用程序是否会进行某种平衡负载吗? I mean, in an OTP application there is the concept of a "worker process" (like a java-ish thread worker)? 我的意思是,在OTP应用程序中有一个“工作进程”的概念(就像一个java-ish线程工作者)?

See modules pg2 and pool . 请参阅模块pg2pool

pg2 implements quite simple distributed process pool. pg2实现了非常简单的分布式进程池。 pg2:get_closest_pid/1 returns "closest" pid, ie random local process if available, otherwise random remote process. pg2:get_closest_pid/1返回“最近”的pid,即随机本地进程(如果可用),否则为随机远程进程。

pool implements load balancing between nodes started with module slave . pool实现了slave模块slave启动的节点之间的负载平衡。

The plists module probably does what you want. plists模块可能会做你想要的。 It is basically a parallel implementation of the lists module, design to be used as a drop-in replacement. 它基本上是lists模块的并行实现,设计用作替代品。 However, you can also control how it parallelizes its operations, for example by defining how many worker processes should be spawned etc. 但是,您也可以控制它如何并行化其操作,例如通过定义应该生成多少个工作进程等。

You probably would do it by calculating some number of workers depending on the length of the list or the load of the system etc. 您可能会通过计算一些工作人员来实现,具体取决于列表的长度或系统的负载等。

From the website: 来自网站:

plists is a drop-in replacement for the Erlang module lists, making most list operations parallel. plists是Erlang模块列表的替代品,使大多数列表操作并行。 It can operate on each element in parallel, for IO-bound operations, on sublists in parallel, for taking advantage of multi-core machines with CPU-bound operations, and across erlang nodes, for parallizing inside a cluster. 它可以并行操作每个元素,对于IO绑定操作,并行子列表,利用具有CPU绑定操作的多核机器,以及跨erlang节点,以便在集群内部进行并行化。 It handles errors and node failures. 它处理错误和节点故障。 It can be configured, tuned, and tweaked to get optimal performance while minimizing overhead. 它可以进行配置,调整和调整,以获得最佳性能,同时最大限度地减少开销。

There is no, in my view, usefull generic load-balancing tool in otp. 在我看来,在otp中没有有用的通用负载平衡工具。 And perhaps it only usefull to have one in specific cases. 也许在特定情况下只有一个是有用的。 It is easy enough to implement one yourself. 很容易自己实现一个。 plists may be useful in the same cases. plists在相同的情况下可能有用。 I do not believe in parallel-libraries as a substitute to the real thing. 我不相信并行库可以替代真实的东西。 Amdahl will haunt you forever if you walk this path. 如果走这条路,阿姆达尔将永远困扰着你。

The right number of worker processes is equal to the number of schedulers. 正确的工作进程数等于调度程序数。 This may vary depending of what other work is done on the system. 这可能会因系统上的其他工作而有所不同。 Use, 采用,

erlang:system_info(schedulers_online) -> NS

to get the number of schedulers. 获取调度程序的数量。

The notion of overhead when flooding the system with an abundance of worker processes is somewhat faulty. 使用大量工作进程充斥系统时的开销概念有些错误。 There is overhead with new processes but not as much as with os-threads. 新进程有开销但不如os-threads多。 The main overhead is message copying between processes, this can be alleviated with the use of binaries since only the reference to the binary is sent. 主要的开销是进程之间的消息复制,这可以通过使用二进制文件来缓解,因为只发送对二进制文件的引用。 With eterms the structure is first expanded then copied to the other process. 使用eterms,首先将结构展开,然后复制到另一个进程。

There is no way how to predict cost of work mechanically without measure it eg do it. 没有办法在没有措施的情况下如何机械地预测工作成本,例如这样做。 Some person must determine how to partition work for some class of tasks. 有些人必须确定如何为某类任务划分工作。 In load balancer word I understand something very different than in your question. 在负载均衡器中,我理解的东西与你的问题非常不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM