出于某种原因,Erlang spawn returns undef

Erlang spawn returns undef for some reason

当 运行 100 个传感器,所有进程 return 某个版本的

时,我收到 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中,所以宏?MODULEmain代替。因此,您要求 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。