如何在Scala中使用actors时限制并发性? [英] How to limit concurrency when using actors in Scala?

查看:135
本文介绍了如何在Scala中使用actors时限制并发性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自Java,我将 Runnable 提交到由线程支持的 ExecutorService 池。在Java中很清楚如何设置线程池大小的限制。



我对使用Scala演员感兴趣,但我不清楚如何限制并发。



让我们假设,我创建一个接受jobs的web服务。使用 POST 请求提交作业,并且我希望我的服务将该作业排入队列,然后立即返回 202接受 - ie



如果我使用actors来处理队列中的作业,那么如何限制处理的同时作业数量? p>

我可以想到几种不同的方法来处理这个问题。我想知道是否有一个社区最佳实践,或者至少,一些明确建立的方法,在Scala世界有些标准。



我想到的一种方法具有单个协调器actor,其将管理作业队列和作业处理操作者;我想它可以使用一个简单的int字段来跟踪当前正在处理多少作业。我敢肯定,那里会有一些方法,但是,如确保跟踪什么时候发生错误,以减少数字。这就是为什么我想知道Scala是否已经提供了一个更简单或更封装的方法。



BTW我试图提出这个问题前一段时间,但我问得很严重。



谢谢!

解决方案

您可以覆盖系统属性 actors.maxPoolSize actors.corePoolSize 这限制了actor线程池的大小,然后在你的角色可以处理的池中抛出尽可能多的作业。您为什么需要调节您的反应?


I'm coming from Java, where I'd submit Runnables to an ExecutorService backed by a thread pool. It's very clear in Java how to set limits to the size of the thread pool.

I'm interested in using Scala actors, but I'm unclear on how to limit concurrency.

Let's just say, hypothetically, that I'm creating a web service which accepts "jobs". A job is submitted with POST requests, and I want my service to enqueue the job then immediately return 202 Accepted — i.e. the jobs are handled asynchronously.

If I'm using actors to process the jobs in the queue, how can I limit the number of simultaneous jobs that are processed?

I can think of a few different ways to approach this; I'm wondering if there's a community best practice, or at least, some clearly established approaches that are somewhat standard in the Scala world.

One approach I've thought of is having a single coordinator actor which would manage the job queue and the job-processing actors; I suppose it could use a simple int field to track how many jobs are currently being processed. I'm sure there'd be some gotchyas with that approach, however, such as making sure to track when an error occurs so as to decrement the number. That's why I'm wondering if Scala already provides a simpler or more encapsulated approach to this.

BTW I tried to ask this question a while ago but I asked it badly.

Thanks!

解决方案

You can override the system properties actors.maxPoolSize and actors.corePoolSize which limit the size of the actor thread pool and then throw as many jobs at the pool as your actors can handle. Why do you think you need to throttle your reactions?

这篇关于如何在Scala中使用actors时限制并发性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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