我有一个存储角色,应该每秒从收集器actor获取新数据。但由于某种原因,循环没有收集。它就像循环根本不起作用。
start(Server)->
spawn(storage,init,[Server]).
init(Server)->
Pid = getWeather:start(self()),
loop(#hourlyHistory{measurements=[]},#cityHistory{measurements = []},Server, Pid).
loop(Hourly = #hourlyHistory{measurements = Hh}, City = #cityHistory{measurements = Ch}, Server, Collector)->
receive
{hourly, {Time, Data}} ->
Measurement = #data{timestamp = Time, data = Data},
case addToListIfNotAlreadyIn(Measurement, Hh) of
{H1, ok} ->
Server ! 'new hourly',
loop(Hourly#hourlyHistory{measurements=H1},City,Server,Collector);
{H1, nok} -> loop(Hourly#hourlyHistory{measurements=H1},City,Server,Collector);
_ -> loop(Hourly,City,Server,Collector)
end
end
addToListIfNotAlreadyIn(New, []) -> {[New], ok};
addToListIfNotAlreadyIn(New, [New]) -> {[New], nok};
addToListIfNotAlreadyIn(New, [Old]) -> {[New | Old], ok};
addToListIfNotAlreadyIn(New ,[New|T]) -> {[New| T], nok};
addToListIfNotAlreadyIn(New, [Old|T]) -> {[New, Old | T], ok}.
这是数据收集器我知道发件人正在工作并在每秒后发送,但存储器没有收到任何内容。
start(Collector)->
H = spawn(getWeather, senderHourly, [Collector]),
C = spawn(getWeather, senderCity, [Collector]),
loop(H,C).
loop(H,C)->
receive
close ->
H ! close,
C ! close
end.
senderHourly(Collector)->
Collector ! {hourly, getHourlyForecast()},
receive
close -> io:format("Closing hourly checker")
after 1000 -> senderHourly(Collector)
end.
这可能是什么问题?
修改
getHourlyForecast()->
inets:start(),
{ok,{_Status,_Header, Body}} = httpc:request(?HOURLY_URL),
{Xml, _Rest} = xmerl_scan:string(Body),
template(Xml).
template(E = #xmlElement{name='observations'})->
{ xmerl_xs:xslapply(fun template/1, xmerl_xs:select("@timestamp",E)),xmerl_xs:xslapply(fun template/1, E)};
template(E = #xmlElement{name='station'})->
["Jaam: " , xmerl_xs:value_of(xmerl_xs:select("name",E)), " Temperature: " ,xmerl_xs:value_of(xmerl_xs:select("airtemperature",E))];
template(E) -> xmerl_xs:built_in_rules(fun template/1, E).
解决
getWeather:start(self())进入循环whiodut产生循环的procces,因此它被卡在那里。如果在getWeather开始我为循环生成一个procces它已得到修复
start(Collector)->
spawn(getWeather,init,[Collector]).
init(Collector)->
H = spawn(getWeather, senderHourly, [Collector]),
C = spawn(getWeather, senderCity, [Collector]),
loop(H,C).