为什么 whereis() return undefined 即使我刚刚注册了进程?
Why does whereis() return undefined even though I just registered the process?
我正在尝试启动一个简单的过程,该过程将循环并接收加法或减法命令来计算它们。然而,当我注册我的进程时,我尝试用 whereis()
打印它以查看它是否启动,并且 运行 和它 returns undefined
。知道出了什么问题吗?
-module(math).
-export([start/0, math/0]).
start() -> register(myprocess, spawn(math, math(), [])).
math() ->
io:format("~p~n", [whereis(myprocess)]),
receive
{add, X, Y} ->
io:format("~p + ~p = ~p~n", [X,Y,X+Y]),
math();
{sub, X, Y} ->
io:format("~p - ~p = ~p~n", [X,Y,X-Y]),
math()
end.
这也是我在 erl shell 中的输入:
Eshell V12.0 (abort with ^G)
1> c(math).
{ok,math}
2> math:start().
undefined
删除传递给 spawn
的 math()
参数上的括号。
spawn
的第二个参数应该是进程名称的原子,但您实际上是在调用该函数并将结果传递给 spawn(所有这些都必须在 register
本身被调用)。
我正在尝试启动一个简单的过程,该过程将循环并接收加法或减法命令来计算它们。然而,当我注册我的进程时,我尝试用 whereis()
打印它以查看它是否启动,并且 运行 和它 returns undefined
。知道出了什么问题吗?
-module(math).
-export([start/0, math/0]).
start() -> register(myprocess, spawn(math, math(), [])).
math() ->
io:format("~p~n", [whereis(myprocess)]),
receive
{add, X, Y} ->
io:format("~p + ~p = ~p~n", [X,Y,X+Y]),
math();
{sub, X, Y} ->
io:format("~p - ~p = ~p~n", [X,Y,X-Y]),
math()
end.
这也是我在 erl shell 中的输入:
Eshell V12.0 (abort with ^G)
1> c(math).
{ok,math}
2> math:start().
undefined
删除传递给 spawn
的 math()
参数上的括号。
spawn
的第二个参数应该是进程名称的原子,但您实际上是在调用该函数并将结果传递给 spawn(所有这些都必须在 register
本身被调用)。