OTP - 同步与异步消息传递 [英] OTP - Synchronous vs. Asynchronous messaging

查看:138
本文介绍了OTP - 同步与异步消息传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:erlang newbie。

Disclaimer: erlang newbie.

首先吸引我erlang的事情之一是Actor模型;不同进程同时运行并通过异步消息交互的想法。

One of the things that attracted me to erlang in the first place is the Actor model; the idea that different processes run concurrently and interact via asynchronous messaging.

我刚刚开始把我的牙齿变成OTP,特别是看gen_server。我所看到的所有例子 - 并且被授予教程类型示例 - 使用 handle_call()而不是 handle_cast()实现模块行为。

I'm just starting to get my teeth into OTP and in particular looking at gen_server. All the examples I've seen - and granted they are tutorial type examples - use handle_call() rather than handle_cast() to implement module behaviour.

我发现有点混乱。据我所知,handle_call是一个同步操作:调用者被阻塞,直到被调用者完成并返回。这似乎与异步消息传递哲学背道而驰。

I find that a little confusing. As far as I can tell, handle_call is a synchronous operation: the caller is blocked until the callee completes and returns. Which seems to run counter to the async message passing philosophy.

我即将开始一个新的OTP应用程序。这似乎是一个基本的建筑决策,所以我想确保我在开始之前了解。

I'm about to start a new OTP application. This seems like a fundamental architectural decision so I want to be sure I understand before embarking.

具体来说,我的问题是:

To be specific, my questions are:


  1. 在实际操作中,人们倾向于使用handle_call而不是handle_cast?

  2. 如果是这样,当多个客户端可以调用相同的流程/模块?

谢谢。

推荐答案


  1. 取决于您的情况。

  1. Depends on your situation.

如果要获取结果, handle_call 真的很常见如果您对通话结果不感兴趣,请使用 handle_cast 。当使用 handle_call 时,调用者将阻止,是的。这是大部分时间还好。让我们来看一个例子。

If you want to get a result, handle_call is really common. If you're not interested in the result of the call, use handle_cast. When handle_call is used, the caller will block, yes. This is most of time okay. Let's take a look at an example.

如果你有一个Web服务器,它会将文件的内容返回给客户端,你将能够处理多个客户端。每个客户端都有等待文件的内容被读取,所以在这种情况下使用 handle_call 将是完美的(愚蠢的例子)

If you have a web server, that returns contents of files to clients, you'll be able to handle multiple clients. Each client have to wait for the contents of files to be read, so using handle_call in such a scenario would be perfectly fine (stupid example aside).

当您真正需要发送请求的行为时,进行一些其他处理,然后稍后收到回复,通常会使用两个调用(例如,一个转换和一个电话获取结果)或正常的消息传递。但是这是一个相当罕见的情况。

When you really need the behavior of sending a request, doing some other processing and then getting the reply later, typically two calls are used (for example, one cast and the one call to get the result) or normal message passing. But this is a fairly rare case.

使用 handle_call 会阻止进程的持续时间呼叫。这将导致客户排队等待他们的回复,因此整个事情将按顺序运行。

Using handle_call will block the process for the duration of the call. This will lead to clients queuing up to get their replies and thus the whole thing will run in sequence.

如果你想要并行代码,你必须编写并行代码。唯一的方法是运行多个进程。

If you want parallel code, you have to write parallel code. The only way to do that is to run multiple processes.

所以,总结一下:


  • 使用 handle_call 将阻止该呼叫者并占用呼叫持续时间内调用的进程。 >
  • 如果要并行活动,您必须并行化。唯一的办法是通过启动更多的进程,并突然打电话给vs不再是一个这么大的问题(实际上,它更适合通话)。

  • Using handle_call will block the caller and occupy the process called for the duration of the call.
  • If you want parallel activities to go on, you have to parallelize. The only way to do that is by starting more processes, and suddenly call vs cast is not such a big issue any more (in fact, it's more comfortable with call).

这篇关于OTP - 同步与异步消息传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆