gpt4 book ai didi

Erlang spawn 由于某种原因返回 undef

转载 作者:行者123 更新时间:2023-12-02 09:05:26 27 4
gpt4 key购买 nike

当我使用 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).

和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.

我做错了什么?我导入了 watcher_run/2 并且应该向它发送 2 个参数。

最佳答案

这是 import 的定义:

-import(Module,Functions).

Imported functions. Can be called the same way as local functions, that is, without any module prefix.

Module, an atom, specifies which module to import functions from. Functions is a list similar as for export.

这一行:

spawn(?MODULE, watcher_start, [SenN, SID]);

出现在模块main中,因此宏?MODULE被替换为main。因此,您要求 erlang 生成一个执行 main:watcher_start(SenN, SID) 的进程。但是,watcher_start/2 并未在 main 模块中定义,而是在 watcher 模块中定义。换句话说,当您将函数导入到模块中时,并不意味着您可以将该函数视为模块的一部分。

修复方法是调用:

spawn(watcher, watcher_start, [SenN, SID]);

或者因为您正在导入watcher_start/2,您可以省略模块名称并调用:

   spawn(
fun() -> watcher_start(SenN, SID) end
).

您在这一行中遇到了同样的问题:

spawn(?MODULE, watcher_start, [10, SID]),

导入模块可能会节省一些打字时间,但是以牺牲代码清晰度为代价的。您可能会考虑永远不要在代码中使用 import。

关于Erlang spawn 由于某种原因返回 undef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59121798/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com