我在运行 100 个传感器时收到 Erlang 的响应,所有进程都返回某个版本的
进程 <0.124.0> 出错,退出值:{undef,[{main,watcher_start,[10,0],[]}]}
我不确定我做错了什么。
这是 main.erl
-module(main).
-import(watcher,[watcher_start/2]).
-import(sensor, [sensor_run/2]).
-compile(export_all).
%given
start() ->
{ok, [ N ]} = io:fread("enter number of sensors> ", "~d"),
if N =< 1 ->
io:fwrite("setup: range must be at least 2~n", []);
true ->
Num_watchers = 1 + (N div 10),
setup_loop(N, Num_watchers)
end.
setup_loop(SenN, Watcher_count) ->
setup_loop(SenN, Watcher_count, 0).
setup_loop(SenN, Watcher_count, SID) when SenN =< 10 ->
spawn(?MODULE, watcher_start, [SenN, SID]);
setup_loop(SenN, Watcher_count, SID) ->
spawn(?MODULE, watcher_start, [10, SID]),
setup_loop(SenN - 10, Watcher_count - 1, SID + 10).
Run Code Online (Sandbox Code Playgroud)
和 watcher.erl
-module(watcher).
-import(sensor, [sensor_run/2]).
-compile(export_all).
watcher_start(SenN, SID) ->
%report start and run the watcher. Generate the list with number of sensors
TheSensorList = gen_list(SenN, SID, []),
io:fwrite("The watcher service is started ~w~n", [TheSensorList]),
watcher_run(TheSensorList).
watcher_run(SensorList) ->
receive
%recieve a measurement, print it out and mainatain the same list as it has not changed
{From, Measurement} ->
io:fwrite("Sensor ~p gave a Measurement of ~p~n", [From, Measurement]),
UpdatedSensorList = SensorList;
%process with PID is down, a sesnor crashed
{'DOWN', _, process, PID, Reason} ->
%Find the sensor ID (SID) of the process that is down
{_, SID} = lists:keyfind(PID, 1, SensorList),
%report the failure that has been deteced
io:fwrite("Sensor ~p has crashed with error ~p i will attempt to restart it~n", [SID, Reason]),
%restart the sensor, the new process will have a new PID & report it
{UpdatedPID, _} = spawn_monitor(sensor, sensor_run, [self(), SID]),
%Now we canm actually updatethe list with a new value
UpdatedSensorList = lists:keyreplace(SID, 2, SensorList, {UpdatedPID, SID}),
io:fwrite("The sensor ~p was restarted with PID ~p", [SID, UpdatedPID])
end,
watcher_run(UpdatedSensorList).
gen_list(SenN, SID, TheList) ->
%create the monitor and continue unil all the SenN number of monitors have been created
{PID, _} = spawn_monitor(sensor, sensor_run, [self(), SID]),
%continue and add the tuple of the new monitor to the list
gen_list(SenN - 1, SID +1, TheList++[{PID, SID}]);
%Base case when we are out of SenN monitors
gen_list(0, _, TheList) ->
TheList.
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我已经导入了 watcher_run/2 并且应该向它发送 2 个参数。
这是import的定义:
-import(模块,功能)。
导入的函数。可以像本地函数一样调用,即不带任何模块前缀。
Module,一个原子,指定从哪个模块导入函数。函数是一个类似于导出的列表。
这一行:
spawn(?MODULE, watcher_start, [SenN, SID]);
Run Code Online (Sandbox Code Playgroud)
出现在模块中main,因此宏?MODULE被替换为main. 因此,您要求 erlang 生成一个执行main:watcher_start(SenN, SID). 但是,watcher_start/2没有在main模块中定义——它在watcher模块中定义。换句话说,当您将函数导入模块时,并不意味着您可以将该函数视为模块的一部分。
解决方法是调用:
spawn(watcher, watcher_start, [SenN, SID]);
Run Code Online (Sandbox Code Playgroud)
或者因为您正在导入,watcher_start/2您可以省略模块名称并调用:
spawn(
fun() -> watcher_start(SenN, SID) end
).
Run Code Online (Sandbox Code Playgroud)
您在这一行中遇到了同样的问题:
spawn(?MODULE, watcher_start, [10, SID]),
Run Code Online (Sandbox Code Playgroud)
导入一个模块可能会节省一些输入,但它是以代码清晰度为代价的。您可能会考虑永远不要在代码中使用 import。
| 归档时间: |
|
| 查看次数: |
109 次 |
| 最近记录: |