混合 Actors 和 DaemonActors 的奇怪/随机行为
这可能与之前的问题有关,但我不太确定......
我有一个基于 Scala/Actor 的子系统,它使用 3 个合作的 Actor 来完成一些工作。每个 Actors 实际上都是一个 DaemonActor。外部消息被发送到主要 Actor,有时消息会从辅助 Actor 发送到主要 Actor,要求它使用从外部消息收集的数据进行操作。
我编写了一个测试驱动程序 Scala 程序,用于启动相关子系统,并使用 DaemonActor 向子系统(即主 Actor)发送消息。
事实证明,发送到主Actor的消息被主Actor处理了,但是从辅助子系统Actor发送到主Actor的消息没有被处理。
我发现,如果我将测试驱动程序中的 Actor 设置为非 Deamon Actor,而不是 DaemonActor,则一切都会按预期进行。这是 100% 确定性的,因为当外部测试驱动程序使用 Actor 时,子系统始终表现良好。当外部测试驱动程序使用 DaemonActor 时,子系统总是表现不佳。在 Actors 和 DaemonActors 之间切换时,没有对代码进行任何其他更改。
让事情变得更奇怪的是,当我制作一个扩展的测试驱动程序,使用 2 个 Actor 向子系统发送 2 种不同类型的消息时,我必须将测试驱动程序的 Actor 之一设置为 DaemonActor,否则接收消息的子系统会行为不当。
看起来很随机:-) 需要
注意的一点是:测试驱动程序参与者实际上调用子系统类上的方法,该子系统类将方法调用“转换”为发送到主要子系统参与者的消息。这是为了与 Java 代码兼容。
我尝试了多种不同的方法来判断消息是否正在处理。然而,我做到了,在程序运行时,我需要从程序中获取一些信息,然后我就开始打印内容。因此,我提到了有关打印和刷新缓冲区的问题线程。唯一影响行为的似乎是 Actor 与 DaemonActor。
我可以发送代码,但会有点多。
任何见解将不胜感激!
This may be related to a previous question, but I am not so sure.......
I have an Scala/Actor-based subsystem that uses 3 cooperating actors to do some work. Each of the Actors is actually a DaemonActor. External messages are sent into a primary Actor, and occasionally messages are sent from a secondary Actor to the primary, asking it to do stuff with the data it collected from the external messages.
I wrote a test-driver Scala program that starts up the subsystem in question, and uses a DaemonActor to send messages to the subsystem (that is to the primary Actor).
It turns out that messages sent into the primary Actor were processed by the primary Actor, but messages sent from the secondary subsystem Actor to the Primary Actor were not processed.
I discovered that if I made the Actor in the test-driver program a non-Deamon Actor, and not a DaemonActor, everything worked as expected. This was 100% deterministic in that when the external test-driver used an Actor, the subsystem always behaved. When the external test-driver used a DaemonActor, the subsystem always mis-behaved. No other changes were made to the code when switching between Actors and DaemonActors.
The make things even stranger, when I made an expanded test driver that used 2 Actors to send 2 different types of messages to the subsystem, I had to make one of the test driver's actor a DaemonActor or the subsystem receiving messages mis-behaved.
Seems pretty random :-)
One caveat to note: The test driver actors actually call methods on a subsystem class which "translates" the method call into a message send to the primary subsystem actor. This is for compatibility with Java code.
I tried a number of different ways to tell if messages where being processed. However I did it I needed some info from the program, while it was running, and I devolved to printing stuff out. Thus my reference to the question thread about printing and flushing buffers. The only thing that seemed to affect behaviour was Actor vs. DaemonActor.
I could send out code, but it would be kind of a lot.
Any insight would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个可能的问题是你还没有启动演员?
One possible problem is that you haven't started the actors?
DaemonActor 的工作流程到底是什么?您使用的是 Scala 2.7 还是 2.8?如果您将代码发布到 gist 或 Pastebin 类型的系统上,我相信我们中的许多人都会很乐意查看它。 :)
在 2.8 中,常规 Actor 会阻止运行时在其活动时终止; DaemonActors,顾名思义,不这样做。如果您只是向 DaemonActor 发送一条或多条消息,然后程序结束,则它可能永远不会达到向其他 Actor 发送消息的程度。
What exactly is the workflow around the DaemonActor, and are you using Scala 2.7 or 2.8? If you post your code on a gist or pastebin-type system, I'm sure many of us would be happy to look at it. :)
In 2.8, regular actors prevent the runtime from terminating while they active; DaemonActors, as the name implies, do not. If you are simply sending the DaemonActor one or more messages and then the program ends, it may never even get to the point of sending messages to the other Actors.