使用演员池有意义吗? [英] Does it make sense to use a pool of Actors?

查看:169
本文介绍了使用演员池有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习,真的很喜欢,Actor模式。我现在使用Scala,但我对建筑风格感兴趣,因为它用于Scala,Erlang,Groovy等。



案例I 我想要的是我需要同时做事情,例如,让我们说运行一份工作。



使用线程,我将创建一个线程池和一个阻塞队列,并让每个线程轮询阻塞队列,并处理作业进入和退出队列。



与演员,什么是最好的方法来处理这个?是否有意义创建一个演员池,并以某种方式发送消息给他们包含或作业?也许与一个协调者的演员?



注意 :我忘了提到的一个方面是:如果我想约束我的应用程序将同时处理的作业数量怎么办?也许配置设置?

解决方案



/ div>

有时,限制在大型任务列表上同时运行多少个工作进程是有意义的,因为进程被派生以完成涉及资源分配的任务。至少,进程占用了内存,但是它们也可以保持打开的文件和/或套接字,这些文件和套接字往往仅限于数千个,并且一旦用完就会失败并且不可预测。



要拥有一个拉动驱动的任务池,可以生成 N 个链接进程来请求任务,一个是他们可以使用spawn_monitor的函数。一旦监视的进程结束,它们就回到下一个任务。具体需要驱动细节,但这是一种方法的概要。



我会让每个任务产生一个新的过程的原因是进程有一些状态,它是很好的开始一个干净的石板。这是一个常见的微调,以设置进程调整的最小堆大小,以最大限度地减少其生命周期中所需的GC数量。这也是一个非常有效的垃圾回收,释放一个进程的所有内存,并开始一个新的进程下一个任务。



使用两倍的数字感觉奇怪的过程?这是一种感觉,你需要克服在Erlang编程。


I'm just learning, and really liking, the Actor pattern. I'm using Scala right now, but I'm interested in the architectural style in general, as it's used in Scala, Erlang, Groovy, etc.

The case I'm thinking of is where I need to do things concurrently, such as, let's say "run a job".

With threading, I would create a thread pool and a blocking queue, and have each thread poll the blocking queue, and process jobs as they came in and out of the queue.

With actors, what's the best way to handle this? Does it make sense to create a pool of actors, and somehow send messages to them containing or the jobs? Maybe with a "coordinator" actor?

Note: An aspect of the case which I forgot to mention was: what if I want to constrain the number of jobs my app will process concurrently? Maybe with a config setting? I was thinking that a pool might make it easy to do this.

Thanks!

解决方案

Sometimes, it makes sense to limit how many working processes you have operating concurrently on a large task list, as the task the process is spawned to complete involve resource allocations. At the very least processes use up memory, but they could also keep open files and/or sockets which tend to be limited to only thousands and fail miserably and unpredictable once you run out.

To have a pull-driven task pool, one can spawn N linked processes that ask for a task, and one hand them a function they can spawn_monitor. As soon as the monitored process has ended, they come back for the next task. Specific needs drive the details, but that is the outline of one approach.

The reason I would let each task spawn a new process is that processes do have some state and it is nice to start off a clean slate. It's a common fine-tuning to set the min-heap size of processes adjusted to minimize the number of GCs needed during its lifetime. It is also a very efficient garbage collection to free all memory for a process and start on a new one for the next task.

Does it feel weird to use twice the number of processes like that? It's a feeling you need to overcome in Erlang programming.

这篇关于使用演员池有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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