[英]How to invoke two actors at a time , i.e. parallel?
I have a scenario where two functionalities run parallel. 我有两个功能并行运行的场景。
Below is sample pseudo code. 下面是示例伪代码。
MainActor{
// retrive company ids
//for each company id i need to run another two different actions simultaniously
tell(A_Actor)
tell(B_Actor)
//if I call above they are calling sequentially i.e. first it runs tell(A_Actor)
//then comes to tell(B_Actor).
//If tell(A_Actor) fails it wont run tell(B_Actor).
}
A_Actor{
// do ingest into a file.
}
B_Actor{
// do ingest into a DB.
}
Question : How to run two functionalities ie tell(A_Actor) & tell(B_Actor) run parallel? 问题:如何运行两个功能,即tell(A_Actor)和tell(B_Actor)并行运行?
The tell
method is asynchronous. tell
方法是异步的。 When you fire a tell
to actorA
, it doesn't wait until actorA
finishes or crashes to execute the next action, which here is to tell actorB
当你火一个
tell
给actorA
,它不会等到actorA
完成或崩溃,执行下一个动作,在这里是要tell actorB
If you need to paralelize the two tell
methods, then you can do the following : 如果您需要将两个
tell
方法同时使用,则可以执行以下操作:
val tellActions = Vector(() => actorA.tell(messageA, senderActor), () => actorB.tell(messageB, senderActor))
tellActions.par.foreach(_.apply())
Note that this is Scala code 请注意,这是Scala代码
This has been pointed out in several comments (including mine), but I felt it deserved an answer. 在一些评论(包括我的评论)中已经指出了这一点,但我认为它应该得到答案。
In short, you need to distinguish between calling the tell
method in parallel with the functionality that the actors execute within their receive methods being executed in parallel. 简而言之,您需要区分并行调用
tell
方法和参与者在并行执行的接收方法中执行的功能。 The functionality will be executed in parallel automatically, and calling the tell
method in parallel doesn't make any sense. 该功能将自动并行执行,并且并行调用
tell
方法没有任何意义。
The code you show will execute the ingest in a file and ingest into the DB in parallel. 您显示的代码将在文件中执行提取,并以并行方式提取到DB中。 This is automatic and requires no action on your part;
这是自动的,不需要您采取任何措施; this is how actors and
tell
works. 这就是演员和
tell
方式。 And, despite what you say, if something goes wrong with the file ingestion it will not affect the ingestion into the DB. 而且,尽管您说了什么,但是如果文件提取出现问题,它将不会影响到数据库的提取。 (Assuming you built the actors and messages correctly, since you don't list their implementation.)
(假设您正确构建了参与者和消息,因为您没有列出它们的实现。)
The tell
method is asynchronous: it returns nearly immediately and doesn't do the actual logic (ingestion in this case): the only thing it does is place the message in the recipient's mailbox. tell
方法是异步的:它几乎立即返回并且不执行实际的逻辑(在这种情况下为输入):它唯一要做的就是将邮件放入收件人的邮箱中。 Ismail's answer, in theory, shows you how you could "invoke tell
" in parallel, but in that example you "sequentially" are creating the array that is used for parallel tells and the whole process will be very inefficient.) His code, while technically doing what you ask, is nonsensical in practice: it accomplishes nothing except slowing the code down significantly. 从理论上讲,Ismail的答案向您展示了如何“并行”调用“
tell
”,但是在该示例中,您“顺序地”创建了用于并行告诉的数组,整个过程效率非常低。)他的代码同时从技术上讲,您的要求在实践中是荒谬的:除了显着降低代码速度之外,它什么都做不到。
In short, I think you either: 简而言之,我认为您要么:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.