简体   繁体   English

如何一次调用两个参与者,即并行?

[英]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 当你火一个tellactorA ,它不会等到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: 简而言之,我认为您要么:

  • Have something fundamentally wrong with your actors and how you are calling them. 演员和您如何称呼他们有根本性的错误。
  • You are actually are executing the functionality in parallel and you just aren't realizing it because you are measuring/observing something incorrectly. 实际上,您实际上是在并行执行功能,而您没有意识到它是因为您正在错误地测量/观察某些东西。

暂无
暂无

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

相关问题 如何在运行时定义JUnit测试超时(即没有注释)? - How to define JUnit test time-out at runtime (i.e. without annotations)? Spring Data Mongo:如何仅保存日期,即不保存时间 - Spring Data Mongo: How to save only date i.e. not time 如何调用所有`Mono <E> `同时 - How to invoke all `Mono<E>` at the same time 是否可以对@BeforeEach 进行参数化,即根据每个@Test 给出的参数调用不同的@BeforeEach? - Is it possible to parametrize a @BeforeEach, i.e. invoke a different @BeforeEach depending on a parameter given by each @Test? 寻找一个好的解决方案来在bean中调用http客户端方法(即MDB,会话) - looking for good solution to invoke http client method in a bean (i.e. MDB, session) 如何并行调用3个REST端点? - How do I Invoke 3 REST endpoints in parallel? Java并行处理; 需要的建议,即在Runnanble / Callable接口上 - Parallel-processing in Java; advice needed i.e. on Runnanble/Callable interfaces 我如何阅读(即理解)此Java数组? - How do I read (i.e. understand) this Java array? 如何在骨架实现(即类)中实现一个接口,其两个实例可以相互依赖? - How to implement an interface, whose two instances can depend on each other, in a skeletal implementation, i.e. class, without downcast? 在没有同步块(即低成本锁)的情况下,如何在一次安全操作中自动检查Java中的两个AtomicBooleans? - How to atomically check TWO AtomicBooleans in Java in one safe operation without a synchronized block (i.e. low cost locks)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM