简体   繁体   English

如何在雅司病中保持状态

[英]How to maintain stateful in yaws

I have some process (spawned) with state. 我有一些与州有关的过程(产生)。

How to maintain simple stateful service in yaws? 如何在雅司病中保持简单的有状态服务? How to implement communication to process in "appmods" erl source file? 如何在“appmods”erl源文件中实现通信处理?

update: let's we have simple process 更新:让我们有简单的过程

start() -> loop(0).

loop(C) ->
  receive 
    {inc} -> loop(C + 1);
    {get, FromPid} -> FromPid ! C, loop(C)
  end.

What is the simplest (trivial: without gen_server, yapp) way to access process from web? 什么是最简单的(琐碎的:没有gen_server,yapp)从Web访问进程的方法?

Maybe, I need a minimal example with gen_server+yapp+yaws / appmods+yaws. 也许,我需要一个最小的例子,gen_server + yapp + yaws / appmods + yaws。

The #arg structure is a very important datastructure for the yaws programmer. #arg结构是雅司程序员非常重要的数据结构。 In the ARG of Yaws out/1 there is a variable that can save user state. 在ARAW of Yaws out / 1中,有一个可以保存用户状态的变量。

"state, %% State for use by users of the out/1 callback" “state,%%状态供out / 1回调的用户使用”

You can get detail info here . 您可以在此处获取详细信息。

There only 2 ways to access a process in Erlang: Either you know its Pid (and the node where you expect the process to be) or You know its registered Name (and the erlang node its expected to be). 在Erlang中只有两种方法可以访问进程:要么你知道它的Pid(以及你期望进程的节点),要么你知道它的注册名称(以及它预期的erlang节点)。

Lets say you have your appmod: 让我们说你有你的appmod:

-module(myappmod).
-export([out/1]).
-include("PATH/TO/YAWS_SERVER/include/yaws_api.hrl").
-include("PATH/TO/YAWS_SERVER/include/yaws.hrl").
out(Arg) -> case check_initial_state(Arg) of unknown -> create_initial_state(); {ok,Value}-> UserPid = list_to_pid(Value), UserPid ! extract_request(Arg), receive Response -> {html,format_response(Response)} after ?TIMEOUT -> {html,"request_timedout"} end end.
check_initial_state(A)-> CookieObject = (A#arg.headers)#headers.cookie, case yaws_api:find_cookie_val("InitialState", CookieObject) of [] -> unkown; Cookie -> {ok,Cookie} end.
extract_request(Arg)->
%% get request from POST Data or Get Data Post__data_proplist = yaws_api:parse_post(Arg), Get_data_proplist = yaws_api:parse_query(Arg), %% do many other things.... Request = remove_request(Post__data_proplist,Get_data_proplist), Request.
That simple set up shows you how you would use processes to keep things about a user. 这个简单的设置向您展示了如何使用流程来保存用户的内容。 However, the use of processes is not good. 但是,使用流程并不好。 Processes do fail, so you need a way of recovering what data they were holding. 进程失败,因此您需要一种方法来恢复他们持有的数据。

A better approach is to have a Data storage about your users and have one gen_server to do the look ups. 更好的方法是拥有一个关于用户的数据存储,并有一个gen_server来进行查找。 You could use Mnesia . 你可以使用Mnesia I do not advise you to use processes on the web to keep user state, no matter what kind of app you are doing, even if its a messaging app. 我不建议您使用Web上的进程来保持用户状态,无论您正在做什么类型的应用程序,即使它是一个消息传递应用程序。 Mnesia or ETS tables can keep state and all you need to do is look up. Mnesia或ETS表可以保持状态,您需要做的就是查找。

Use a better storage mechanism to keep state other than processes. 使用更好的存储机制来保持除进程之外的状态。 Processes are a point of failure. 流程是一个失败点。 Others use Cookies (and/or Session cookies), whose value is used in some way to look up something from a database. 其他人使用Cookie(和/或会话cookie),其价值以某种方式用于从数据库中查找某些内容。 However, if you insist that you need processes, then, have a way of remembering their Pids or registered names. 但是,如果您坚持要求进程,那么,有办法记住他们的Pids或注册名称。 You could store a user Pid into their session cookie etc 您可以将用户Pid存储到其会话cookie等中

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM