简体   繁体   English

何时创建Akka演员

[英]When to create an Akka Actor

I have a REST service which services only one POST request. 我有一个REST服务,仅服务一个POST请求。 I want to use an actor to process the request. 我想使用一个演员来处理请求。 However I don't know if I should create one actor and derive all the requests using this actor or should I create an actor every time I get a request. 但是我不知道我应该创建一个actor并使用该actor派生所有请求,还是应该在每次请求时创建一个actor。 What are the pros and cons of these choices. 这些选择的优缺点是什么。 Also, how is it parallel execution when I create one actor and use that actor to process all my requests. 另外,当我创建一个参与者并使用该参与者处理所有请求时,并行执行如何实现。 It certainly looks like sequential execution. 它肯定看起来像顺序执行。 I would want to understand this as well. 我也想了解这一点。

If you use one Actor requests are queued inside the actor mail box and are processed one by one by the actor. 如果使用一个Actor,则Actor请求将排队在actor邮箱中,并由actor逐一处理。 This is sequential and not recommended. 这是顺序的,不建议这样做。

Thats why it is said 那就是为什么这么说

One actor is no actor. 一个演员不是演员。

Create a manager Actor which manages other actors. 创建一个管理其他演员的经理演员。 As actors are quite cheap you can create one actor for every request without any problem. 由于角色非常便宜,因此您可以为每个请求创建一个角色,而不会出现任何问题。 Do db interactions and other heavy computation using a future and direct results of the future to request handling actor using pipeTo pattern. 使用将来进行数据库交互和其他繁重的计算,并使用将来的直接结果来使用pipeTo模式请求处理pipeTo

Use actors only to divide and distribute work and use Futures to do compute intensive work. 仅使用参与者来划分和分配工作,并使用期货来进行计算密集型工作。

I agree with what @pamu said. 我同意@pamu所说的。 Actors are cheap. 演员很便宜。 But be mindful that if ever you are gonna use a singleton Actor, do not make it stateful it will cause trouble. 但是请注意,如果您要使用单例Actor,请不要使其有状态,否则会造成麻烦。

And if you are gonna use Futures to do intensive work (which you should do). 而且,如果您要使用期货做大量的工作(应该做)。 Make sure you give them specific ExecutionContext / Dispatcher. 确保为他们提供特定的ExecutionContext / Dispatcher。 Using the global dispatcher or ExecutionContext is not good. 使用全局调度程序或ExecutionContext不好。

Or in each api you have, create a certain dispatcher to control the # of Actors that will work on that kind of endpoint / api. 或者,在您拥有的每个api中,创建一个特定的调度程序来控制将在这种终结点/ api上工作的Actor的数量。

For example you have "/get/transactions" 例如,您有“ / get / transactions”

specify a dispatcher that would only spawn this # of thread. 指定只会产生此#线程的调度程序。 For this api. 对于此api。

The advantage of this is you can control the # of threads and resources your app uses. 这样做的好处是您可以控制应用程序使用的线程和资源的数量。 When it comes to dealing with heavy traffic. 当涉及到交通繁忙时。 This is a good practice. 这是一个好习惯。

I would create an actor per request and use the "tell" pattern to delegate the work to the newly created actor. 我将为每个请求创建一个演员,并使用“告诉”模式将工作委派给新创建的演员。 If the REST framework you use supports completing the request from another actor (Spray, Akka-HTTP does), then you can complete the request from this new actor. 如果您使用的REST框架支持完成来自其他角色的请求(Spray,Akka-HTTP则支持),那么您可以完成来自此新角色的请求。 This way your request handling actor is free to handle the next request. 这样,您的请求处理角色便可以自由处理下一个请求。

I find this a wonderful resource that explains the pros & cons of ask & tell and per-request-actors. 我发现是一个很好的资源,解释了Ask&Tell和每个请求者的利弊。 It can be helpful to you. 对您有帮助。

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

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