如何发送消息在YAWS / Erlang接收 [英] How to send message to receive in YAWS/Erlang

查看:202
本文介绍了如何发送消息在YAWS / Erlang接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在Erlang程序员中使用符号发送消息以在并发编程中接收,但是我们如何在yaws中执行呢?说我试着做这个>

 < erl> 
out(Arg) - >循环(坏)。


loop(X) - >
receive
good - > {html,Good};
bad - > {html,bad}
end。
< / erl>

此程式一直等待邮件,如何传送邮件?

解决方案

如果你想让一个进程向另一个进程发送消息,很明显你需要两个进程。当Yaws接收到一个HTTP请求时,默认情况下它会将请求分派到其Erlang进程池中的其中一个进程。当你在你的例子中使用 .yaws 文件时,该进程调用你的 out / 1 函数。但这只是一个过程,所以你需要另一个过程。



有很多方法可以启动第二个过程。一个简单的方法是 spawn_link 一个进程运行任何逻辑将发送消息到 loop / 1

 < erl> 
out(_Arg) - >
process_flag(trap_exit,true),
Self = self(),
Pid = spawn_link(fun() - > my_business_logic(Self)end),
loop 。

loop(Pid) - >
receive
{Pid,good} - > {html,Good};
{Pid,bad} - > {html,Bad};
{'EXIT',Pid,Reason} - >
[{status,500},
{html,内部服务器错误}]
结束。

my_business_logic(Parent) - >
%%在这里运行你的逻辑,然后发送消息
父级! {self(),good}。
< / erl>请注意,我们将子进程 Pid EXIT 并正确报告错误。



但这可能不是一个好的方法。如果逻辑过程应该独立于任何HTTP请求运行,那么当您的Erlang系统启动时,您可以启动一个池,并且 out / 1 函数发送一个消息到请它代表它提出请求。这一切取决于这些进程正在做什么,它们如何与传入的请求相关,以及是否有足够的池来满足您期望的请求负载。



使用 .yaws 文件对于某些应用程序很方便,但它们可以是限制性的。另一种方法是构建一个包含Yaws和您自己的应用程序的Erlang系统,并使用Yaws appmod 特性让Yaws将请求分派到运行您自己的Erlang模块的自己的进程中。在这里解释所有这些都不切实际,因此请参阅 Yaws文档 Yaws书,或要求 Yaws邮件列表


Normally in Erlang programmers use ! symbol to send message to receive in concurrent programming but how do we do it in yaws? Say I am trying to do this>

<erl>
out(Arg) -> loop("bad").


loop(X)->
    receive
        good -> {html, "Good"};
        bad  -> {html, "bad"}
    end.
</erl>

This program keeps waiting for a message, How do I send message to it?

解决方案

If you want to have one process send a message to another, it's clear you need two processes. When Yaws receives a HTTP request, by default it dispatches the request into one of its processes in its Erlang process pool. When you're using a .yaws file as in your example, that process invokes your out/1 function. But that's just one process, so you need another.

There are numerous ways to start a second process. One simple way is to spawn_link a process to run whatever logic will send the message to loop/1:

<erl>
    out(_Arg) ->
        process_flag(trap_exit, true),
        Self = self(),
        Pid = spawn_link(fun() -> my_business_logic(Self) end),
        loop(Pid).

    loop(Pid) ->
        receive
            {Pid, good} -> {html, "Good"};
            {Pid, bad} -> {html, "Bad"};
            {'EXIT', Pid, Reason} ->
                [{status, 500},
                 {html, "internal server error"}]
        end.

    my_business_logic(Parent) ->
        %% run your logic here, then send a message
        Parent ! {self(), good}.
</erl>

Note that we put the child process Pid in the message to identify that it's originating from the expected process. Note also that we link to the child process and trap exits so that if the child dies unexpectedly, we can catch the EXIT and report the error properly.

But this might not be a good approach. If the logic process should run independently of any HTTP request, you could start a pool of them when your Erlang system starts, and have the out/1 function send one a message to ask it to carry out a request on its behalf. It all depends on what those processes are doing, how they relate to incoming requests, and whether having a pool of them is adequate for the request load you're expecting.

Using a .yaws file is handy for some applications but they can be limiting. An alternative is to build an Erlang system containing Yaws and your own application, and use the Yaws appmod feature to have Yaws dispatch requests into your own processes running your own Erlang modules. Explaining all that here isn't practical, so please consult the Yaws documentation or the Yaws book, or ask for help from the Yaws mailing list.

这篇关于如何发送消息在YAWS / Erlang接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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