From: Russell B. <rus...@ma...> - 2008-08-23 12:44:22
|
Oops. I sent direct to Edwin rather than to the list. Turns out this is not a YAWS issue at all but a noob-not-understanding- random-module-of-erlang issue. Now the n_bonds module calls random:seed(A, B , B) there is no obvious pattern to the results and they are different for each request. Thanks for the help in getting this far. Regards Russell Begin forwarded message: > From: Russell Brown <rus...@ma...> > Date: 23 August 2008 09:28:46 BST > To: Edwin Fine <em...@gm...> > Subject: Re: [Erlyaws-list] YAWS noob: first yaws app. Caching?? > > Hi edwin, > First up, thanks for taking so much time to help me with my problem. > Going to the lengths you have gone is above and beyond and all that > so I really am very grateful. > > I tried the inets:start(). http:request("http://wombat.me:8000/bonds/10 > "). as you suggested and I get the same response everytime. What has > piqued my interest is you mention of random numbers. Yes I use > random numbers to shuffle the bonds square. So I created a number of > processes to run bonds_square_for_xml and each process gets the same > result the first call and each process gets the same result for the > second call (but different from the first call). So I guess YAWS > creates a process per request and so each process gets the first > "random" shuffle in the sequence. Hmmm. I need to learn more about > how random works don't I? What successive calls to > bonds_square_for_xml get is a sequence of shuffled bonds. But the > sequence is all the same. It always starts with the same order of > bonds and so on. > > You have been a real help. Thank you very much. > > I have to take my son to football (soccer) now but I will read up on > erlang random numbers when I get back. > > Thanks again > > Russell > > PS you can have a look at the "game" at wombat.me:8000 to see whay I > am doing this if you like. > On 22 Aug 2008, at 22:59, Edwin Fine wrote: > >> Russell, >> >> I was intrigued by this so I decided to do a small test appmod that >> works like yours. Ok, I know I should not be manually creating XML, >> but this *is* a test :) >> >> -module(getxml). >> -export([out/1]). >> >> out(_A) -> >> {content, "application/xml", somexml()}. >> >> somexml() -> >> {_,_,U} = Now = now(), >> {{Y,Mo,D},{H,M,S}} = calendar:now_to_local_time(Now), >> Host = net_adm:localhost(), >> io_lib:format( >> "<info>\n" >> " <host name='~s'/>\n" >> " <date year='~B' mon='~B' day='~B'/>\n" >> " <time hour='~B' min='~B' sec='~B' usec='~B'/>\n" >> "</info>\n", >> [Host, Y, Mo, D, H, M, S, U] >> ). >> >> I compiled this in /tmp: >> >> erlc -W getxml.erl >> >> Then I copied the standard yaws.conf to /tmp, and added this >> virtual server config (the rest of yaws.conf is the same except I >> removed all the other virtual server configs). >> >> <server ender> >> port = 8080 >> listen = 0.0.0.0 >> docroot = /tmp >> appmods = <getxml, getxml> >> </server> >> >> Then I started Yaws in interactive mode. >> >> yaws -i --conf /tmp/yaws.conf >> >> I used inets (http:request) to post to the URL http://ender:8080/getxml >> : >> >> 1> inets:start(). >> ok >> 2> http:request("http://ender:8080/getxml"). >> {ok,{{"HTTP/1.1",200,"OK"}, >> [{"date","Fri, 22 Aug 2008 21:54:00 GMT"}, >> {"server","Yaws/1.77 Yet Another Web Server"}, >> {"content-length","137"}, >> {"content-type","application/xml"}], >> "<info>\n <host name='example.com'/>\n <date year='2008' >> mon='8' day='22'/>\n <time hour='17' min='54' sec='0' usec='37845'/ >> >\n</info>\n\n"}} >> 3> http:request("http://ender:8080/getxml"). >> {ok,{{"HTTP/1.1",200,"OK"}, >> [{"date","Fri, 22 Aug 2008 21:54:02 GMT"}, >> {"server","Yaws/1.77 Yet Another Web Server"}, >> {"content-length","138"}, >> {"content-type","application/xml"}], >> "<info>\n <host name='example.com'/>\n <date year='2008' >> mon='8' day='22'/>\n <time hour='17' min='54' sec='2' >> usec='990860'/>\n</info>\n\n"}} >> 4> http:request("http://ender:8080/getxml"). >> {ok,{{"HTTP/1.1",200,"OK"}, >> [{"date","Fri, 22 Aug 2008 21:54:03 GMT"}, >> {"server","Yaws/1.77 Yet Another Web Server"}, >> {"content-length","138"}, >> {"content-type","application/xml"}], >> "<info>\n <host name='example.com'/>\n <date year='2008' >> mon='8' day='22'/>\n <time hour='17' min='54' sec='3' >> usec='340745'/>\n</info>\n\n"}} >> 5> http:request("http://ender:8080/getxml"). >> {ok,{{"HTTP/1.1",200,"OK"}, >> [{"date","Fri, 22 Aug 2008 21:54:04 GMT"}, >> {"server","Yaws/1.77 Yet Another Web Server"}, >> {"content-length","138"}, >> {"content-type","application/xml"}], >> "<info>\n <host name='example.com'/>\n <date year='2008' >> mon='8' day='22'/>\n <time hour='17' min='54' sec='4' >> usec='117845'/>\n</info>\n\n"}} >> >> I used curl, too: >> >> efine@ender:/tmp$ curl http://ender:8080/getxml >> <info> >> <host name='example.com'/> >> <date year='2008' mon='8' day='22'/> >> <time hour='17' min='55' sec='9' usec='725669'/> >> </info> >> >> efine@ender:/tmp$ curl http://ender:8080/getxml >> <info> >> <host name='example.com'/> >> <date year='2008' mon='8' day='22'/> >> <time hour='17' min='55' sec='10' usec='821024'/> >> </info> >> >> As you can see, it changes every time. I later requested the URL >> 100 times in a tight loop, and every return was different. I even >> used Firefox and pressed F5 repeatedly. Different result every >> time. Is there any possibility that your nbonds module behaves >> differently when run in the context of Yaws than when run from the >> shell? Do you use random numbers somewhere? >> >> Could you maybe try the getxml appmod and see if it works for you? >> Also take a look at the output of Yaws when you run it in >> interactive (-i) mode. Any error messages? >> >> Hope this helps. >> >> Ed >> >> On Fri, Aug 22, 2008 at 4:57 PM, Edwin Fine <em...@gm...> >> wrote: >> Russell, >> >> The thing is, I use Yaws with appmods a lot in my work, and do >> something similar (use a browser to HTTP GET run-time statistics >> for an application) and I do in fact get different responses every >> time. That's why I don't think it's necessarily Yaws-related. I am >> using Yaws 1.77, Erlang R12B-3, Ubuntu Linux x86_64. Then again, >> in the case of the GET, I am returning HTML, not XML. Hmmm. >> >> Have you tried sending the GET using something other than a browser >> (for example, wget or curl?). I'd be very interested in seeing the >> full headers and all that you get back. Are you using Linux or >> MacOS (PLEASE say yes...!)? What's your setup? >> >> Please could you try this for me in the shell: >> >> inets:start(), http:request("http://wombat.me:8000/bonds/10"). >> >> Maybe you can send the request a few times (you only need to call >> inets:start() once). I'd be interested in seeing the raw responses... >> >> Also, what's your yaws.conf? >> >> Ed >> >> >> On Fri, Aug 22, 2008 at 3:05 PM, Russell Brown >> <rus...@ma...> wrote: >> Hi Edwin, >> Thanks for the response. >> >> I understand you asking me question 1. I have tried a few times >> just to check. Yup...it get a different result. Here is a session:- >> >> 2> n_bonds:bonds_square_for_xml(10). >> {bonds,[{for,"10"},{total,"16"}], >> [{bond,[{b1,10},{b2,0}],["10+0"]}, >> {bond,[{b1,1},{b2,8}],["1+8"]}, >> {bond,[{b1,8},{b2,2}],["8+2"]}, >> {bond,[{b1,2},{b2,8}],["2+8"]}, >> {bond,[{b1,0},{b2,10}],["0+10"]}, >> {bond,[{b1,6},{b2,4}],["6+4"]}, >> {bond,[{b1,0},{b2,9}],["0+9"]}, >> {bond,[{b1,7},{b2,3}],["7+3"]}, >> {bond,[{b1,5},{b2,4}],["5+4"]}, >> {bond,[{b1,9},{b2,0}],["9+0"]}, >> {bond,[{b1,3},{b2,7}],["3+7"]}, >> {bond,[{b1,1},{b2,9}],["1+9"]}, >> {bond,[{b1,5},{b2,5}],["5+5"]}, >> {bond,[{b1,9},{b2,1}],["9+1"]}, >> {bond,[{b1,4},{b2,6}],["4+6"]}, >> {bond,[{b1,3},{b2,8}],["3+8"]}]} >> 3> n_bonds:bonds_square_for_xml(10). >> {bonds,[{for,"10"},{total,"16"}], >> [{bond,[{b1,9},{b2,1}],["9+1"]}, >> {bond,[{b1,4},{b2,6}],["4+6"]}, >> {bond,[{b1,3},{b2,7}],["3+7"]}, >> {bond,[{b1,2},{b2,8}],["2+8"]}, >> {bond,[{b1,2},{b2,9}],["2+9"]}, >> {bond,[{b1,5},{b2,5}],["5+5"]}, >> {bond,[{b1,0},{b2,10}],["0+10"]}, >> {bond,[{b1,1},{b2,9}],["1+9"]}, >> {bond,[{b1,6},{b2,4}],["6+4"]}, >> {bond,[{b1,7},{b2,3}],["7+3"]}, >> {bond,[{b1,8},{b2,2}],["8+2"]}, >> {bond,[{b1,4},{b2,5}],["4+5"]}, >> {bond,[{b1,8},{b2,3}],["8+3"]}, >> {bond,[{b1,6},{b2,5}],["6+5"]}, >> {bond,[{b1,0},{b2,9}],["0+9"]}, >> {bond,[{b1,10},{b2,0}],["10+0"]}]} >> 4> n_bonds:bonds_square_for_xml(10). >> {bonds,[{for,"10"},{total,"16"}], >> [{bond,[{b1,2},{b2,7}],["2+7"]}, >> {bond,[{b1,10},{b2,0}],["10+0"]}, >> {bond,[{b1,8},{b2,3}],["8+3"]}, >> {bond,[{b1,5},{b2,4}],["5+4"]}, >> {bond,[{b1,2},{b2,8}],["2+8"]}, >> {bond,[{b1,8},{b2,2}],["8+2"]}, >> {bond,[{b1,4},{b2,6}],["4+6"]}, >> {bond,[{b1,1},{b2,9}],["1+9"]}, >> {bond,[{b1,8},{b2,1}],["8+1"]}, >> {bond,[{b1,7},{b2,3}],["7+3"]}, >> {bond,[{b1,0},{b2,10}],["0+10"]}, >> {bond,[{b1,5},{b2,5}],["5+5"]}, >> {bond,[{b1,3},{b2,6}],["3+6"]}, >> {bond,[{b1,3},{b2,7}],["3+7"]}, >> {bond,[{b1,6},{b2,4}],["6+4"]}, >> {bond,[{b1,9},{b2,1}],["9+1"]}]} >> >> >> >> I tried the suggestions you gave but I was and still am pretty sure >> that it is not the browser that is doing the caching. Even with the >> cache headers set the behavior is the same. I recompiled the >> n_bonds module and for the first run I got different results but >> now they are the same each subsequent run. Curious. >> >> I suppose it is not a massive issue since this is just a toy for my >> son but I am pretty curious as to what is going on. i think I am >> going to have to (try) and read the source code. >> >> Cheers again >> >> Russell >> >> On 22 Aug 2008, at 19:17, Edwin Fine wrote: >> >>> Oops, that should have been >>> >>> http://wombat.me:8000/bonds?n=10&dummy=1 >>> >>> not >>> >>> http://wombat.me:8000/bonds/n=10&dummy=1 >>> >>> On Fri, Aug 22, 2008 at 2:14 PM, Edwin Fine <em...@gm...> >>> wrote: >>> Hi Russell, >>> >>> I'm not a Yaws expert but have used it a bit. I can think of a few >>> questions: >>> I know this is obvious but I want to ask it anyway: If you call >>> the function n_bonds:bonds_square_for_xml/1 "manually" using the >>> shell, does it come back with different answers every time? Are >>> you sure that the function itself is working like it should? >>> If yes (and I am sure it is yes), then maybe you can force caching >>> off as follows: >>> out(A) -> >>> Bonds = >>> n_bonds:bonds_square_for_xml(list_to_integer(A#arg.appmoddata)), >>> Export = xmerl:export_simple([Bonds],xmerl_xml), >>> [ >>> {header, {cache_control, "no-cache, must-revalidate"}}, >>> {header, "Expires: Mon, 26 Jul 1997 05:00:00 GMT"}, >>> >>> {content, "application/xml", lists:flatten(Export)} >>> ]. >>> >>> Another easy trick the HTML people use to avoid caching GETs is to >>> make a varying query in the URL, for example >>> >>> http://wombat.me:8000/bonds/n=10&dummy=1 >>> http://wombat.me:8000/bonds/n=10&dummy=2 >>> http://wombat.me:8000/bonds/n=10&dummy=3 >>> >>> Then all you have to do extra in out(A) is parse the query to get >>> "n": >>> >>> out(A) -> >>> QueryList = yaws_api:parse_query(A), % e.g. [{"n","10"}, >>> {"dummy", "1"}] >>> Content = case proplists:get_value("n", QueryList) of >>> undefined -> >>> {content, "application/xml", "<error>Bad query string</ >>> error>"}; >>> NStr -> >>> N = list_to_integer(NStr) , >>> Bonds = n_bonds:bonds_square_for_xml(N), >>> lists:flatten(xmerl:export_simple([Bonds],xmerl_xml)) >>> end, >>> {content, "application/xml", Content}. >>> >>> Hope this helps. >>> >>> Edwin >>> >>> >>> >>> >>> ContentFun( >>> formatted_process_list( >>> ProcInfo, >>> ResponseFun, >>> ErrFun)) >>> ]. >>> >>> >>> >>> >>> On Fri, Aug 22, 2008 at 10:06 AM, Russell Brown <rus...@ma... >>> > wrote: >>> Hi, >>> I am new to YAWS and erlang. I have knocked up a quick toy >>> application >>> for my son to practice his "number bonds" (a maths teaching method >>> for >>> primary school in the UK). I am using an appmod. The code is below:- >>> >>> -module(bonds_yaws). >>> -export([out/1]). >>> -include("/home/russell/erlang/yaws/lib/yaws/include/yaws_api.hrl"). >>> >>> >>> out(A) -> >>> Bonds = >>> n_bonds:bonds_square_for_xml(list_to_integer(A#arg.appmoddata)), >>> Export=xmerl:export_simple([Bonds],xmerl_xml), >>> {content, "application/xml", lists:flatten(Export)}. >>> >>> >>> The code for n_bonds:bonds_square_for_xml(N) takes an integer and >>> generates a tuple that I turn into xml and then YAWS sends it to the >>> client. n_bonds:bonds_square_for_xml produces a term like this >>> >>> {bonds,[{for,"10"},{total,"16"}], >>> [{bond,[{b1,10},{b2,0}],["10+0"]}, >>> {bond,[{b1,8},{b2,2}],["8+2"]}, >>> {bond,[{b1,6},{b2,4}],["6+4"]}, >>> {bond,[{b1,1},{b2,9}],["1+9"]}, >>> {bond,[{b1,3},{b2,7}],["3+7"]}, >>> {bond,[{b1,5},{b2,5}],["5+5"]}, >>> {bond,[{b1,2},{b2,9}],["2+9"]}, >>> {bond,[{b1,1},{b2,8}],["1+8"]}, >>> {bond,[{b1,4},{b2,6}],["4+6"]}, >>> {bond,[{b1,3},{b2,6}],["3+6"]}, >>> {bond,[{b1,3},{b2,8}],["3+8"]}, >>> {bond,[{b1,5},{b2,4}],["5+4"]}, >>> {bond,[{b1,2},{b2,8}],["2+8"]}, >>> {bond,[{b1,7},{b2,3}],["7+3"]}, >>> {bond,[{b1,9},{b2,1}],["9+1"]}, >>> {bond,[{b1,0},{b2,10}],["0+10"]}]} >>> >>> >>> Basically a set of bonds. The a called function of >>> bonds_square_for_xml >>> shuffles the order of the bond rows. The shuffle works well enough >>> so >>> that if you call n_bonds:bonds_square_for_xml(10) twice in a row you >>> will probably get the bonds in a different order and also probably >>> get >>> different data too (there are some incorrect answers thrown in for >>> good >>> measure). However if you hit the Yaws url a few times you *always* >>> get >>> the same answer. So http://wombat.me:8000/bonds/10 always shows >>> the same >>> xml. >>> >>> Does YAWS cache the result of executing the erlang? Can I stop it >>> from >>> doing that? How? >>> >>> Many thanks if you can help. >>> >>> Cheers >>> >>> Russell >>> >>> ------------------------------------------------------------------------- >>> This SF.Net email is sponsored by the Moblin Your Move Developer's >>> challenge >>> Build the coolest Linux based applications with Moblin SDK & win >>> great prizes >>> Grand prize is a trip for two to an Open Source event anywhere in >>> the world >>> http://moblin-contest.org/redirect.php?banner_id=100&url=/ >>> _______________________________________________ >>> Erlyaws-list mailing list >>> Erl...@li... >>> https://lists.sourceforge.net/lists/listinfo/erlyaws-list >>> >>> >>> >>> >>> -- >>> For every expert there is an equal and opposite expert - Arthur C. >>> Clarke >>> >>> >>> >>> -- >>> For every expert there is an equal and opposite expert - Arthur C. >>> Clarke >> >> >> ------------------------------------------------------------------------- >> This SF.Net email is sponsored by the Moblin Your Move Developer's >> challenge >> Build the coolest Linux based applications with Moblin SDK & win >> great prizes >> Grand prize is a trip for two to an Open Source event anywhere in >> the world >> http://moblin-contest.org/redirect.php?banner_id=100&url=/ >> _______________________________________________ >> Erlyaws-list mailing list >> Erl...@li... >> https://lists.sourceforge.net/lists/listinfo/erlyaws-list >> >> >> >> >> -- >> For every expert there is an equal and opposite expert - Arthur C. >> Clarke >> >> >> >> -- >> For every expert there is an equal and opposite expert - Arthur C. >> Clarke > |