scala 我是否需要重复使用相同的 Akka ActorSystem 还是可以在每次需要时都创建一个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10396552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do I need to re-use the same Akka ActorSystem or can I just create one every time I need one?
提问by sroebuck
Akka 2.x requires many commands to reference an ActorSystem. So, to create an instance of an actor MyActoryou might say:
Akka 2.x 需要许多命令来引用ActorSystem. 因此,要创建 actor 的实例,MyActor您可能会说:
val system = ActorSystem()
val myActor = system.actorOf(Props[MyActor])
Because of the frequent need for an ActorSystem: many code examples omit the creation from the code and assume that the reader knows where a systemvariable has come from.
因为经常需要ActorSystem: 许多代码示例省略了代码中的创建,并假设读者知道system变量来自哪里。
If your code produces actors in different places, you could duplicate this code, possibly creating additional ActorSysteminstances, or you could try to share the same ActorSysteminstance by referring to some global or by passing the ActorSystemaround.
如果您的代码在不同的地方生成actor,您可以复制此代码,可能会创建额外的ActorSystem实例,或者您可以尝试ActorSystem通过引用某个全局或通过传递来共享相同的实例ActorSystem。
The Akka documentation provides a general overview of systems of actorsunder the heading 'Actor Systems', and there is documentation of the ActorSystemclass. But neither of these help a great deal in explaining why a user of Akka can't just rely on Akka to manage this under-the-hood.
Akka 文档在“演员系统”标题下提供了演员系统的一般概述,并且有类的文档ActorSystem。但是这些都没有帮助解释为什么 Akka 的用户不能仅仅依靠 Akka 来管理这个底层。
Question(s)
问题)
What are the implications of sharing the same
ActorSystemobject or creating a new one each time?What are the best practices here? Passing around an
ActorSystemall the time seems surprisingly heavy-handed.Some examples give the
ActorSystema name:ActorSystem("MySystem")others just callActorSystem(). What difference does this make, and what if you use the same name twice?Does
akka-testkitrequire that you share a commonActorSystemwith the one you pass to theTestKitconstructor?
ActorSystem每次共享同一个对象或创建一个新对象有什么含义?这里的最佳做法是什么?一直在周围传递
ActorSystem似乎出人意料地严厉。一些例子给出了
ActorSystem一个名字:ActorSystem("MySystem")其他的只是调用ActorSystem(). 这有什么区别,如果您使用相同的名称两次会怎样?是否
akka-testkit要求您ActorSystem与传递给TestKit构造函数的人共享一个共同点?
采纳答案by drexin
Creating an ActorSystem is very expensive, so you want to avoid creating a new one each time you need it. Also your actors should run in the same ActorSystem, unless there is a good reason for them not to. The name of the ActorSystem is also part the the path to the actors that run in it. E.g. if you create an actor in a system named MySystemit will have a path like akka://MySystem/user/$a. If you are in an actor context, you always have a reference to the ActorSystem. In an Actor you can call context.system. I don't know what akka-testkit expects, but you could take a look at the akka tests.
创建 ActorSystem 非常昂贵,因此您希望避免每次需要时都创建一个新的。此外,您的演员应该在同一个 ActorSystem 中运行,除非他们有充分的理由不这样做。ActorSystem 的名称也是其中运行的 actor 路径的一部分。例如,如果您在名为MySystem它的系统中创建一个角色,它将具有类似akka://MySystem/user/$a. 如果您处于演员上下文中,则始终拥有对 ActorSystem 的引用。在 Actor 中,您可以调用context.system. 我不知道 akka-testkit 期望什么,但你可以看看 akka 测试。
So to sum it up, you should always use the same system, unless there is a good reason not to do so.
总而言之,您应该始终使用相同的系统,除非有充分的理由不这样做。
回答by Haimei
Here are some materials which might be helpful to understand "Why does document always suggest to use one ActorSystem for one logical application" :
以下是一些可能有助于理解“为什么文档总是建议将一个 ActorSystem 用于一个逻辑应用程序”的材料:
The heaviest part of an ActorSystem is the dispatcher. Each ActorSystem has at least one. The dispatcher is the engine that makes the actors running. In order to make running, it needs threads (usually got from a thread pool). The default dispatcher uses a fork-join thread pool with at least 8 threads.
There are shared facilities, like the guardian actors, the event stream, the scheduler, etc. Some of them are in user space, some are internal. All of them need to be created and started.
One ActorSystem with one thread pool configures to the numbers of cores should give the best results in most cases.
Here document mentions logical application, I prefer to consider blocking or non-blocking application. According to dispatcher's configuration, one ActorSystem is for one configuration. If the application is for the same logics, one ActorSystem should be enough.
ActorSystem 最重要的部分是调度程序。每个 ActorSystem 至少有一个。调度器是使actor 运行的引擎。为了运行,它需要线程(通常从线程池中获取)。默认调度程序使用至少有 8 个线程的 fork-join 线程池。
有共享设施,如监护人actor、事件流、调度程序等。其中一些在用户空间,一些在内部。所有这些都需要创建和启动。
在大多数情况下,一个带有一个线程池的 ActorSystem 配置为内核数量应该会给出最好的结果。
这里文档提到了逻辑应用程序,我更喜欢考虑阻塞或非阻塞应用程序。根据调度员的配置,一个ActorSystem对应一个配置。如果应用程序是相同的逻辑,一个 ActorSystem 应该就足够了。
Here is a discussion, if you have time, you can read it. They discuss a lot, ActorSystem, local or remote, etc.
这是一个讨论,如果你有时间,你可以阅读它。他们讨论了很多,ActorSystem,本地还是远程等等。

