Erlang:无法创建 gen_server:call()

Posted

技术标签:

【中文标题】Erlang:无法创建 gen_server:call()【英文标题】:Erlang: Fail to make a gen_server:call() 【发布时间】:2021-03-14 21:47:31 【问题描述】:

给定以下代码:

-module(appender_server).
-behaviour(gen_server).

-export([start_link/1, stop/0]).
-export([append_synched/2]).
-export([init/1, handle_call/3, handle_cast/2]).

start_link(FilePath) ->
    gen_server:start_link(local, ?MODULE, ?MODULE, FilePath, []).

init(FilePath) ->
    ok, TheFile = file:open(FilePath, [append]), % File is created if it does not exist.
    io_lib:print(TheFile).

append_synched(FilePath, Text) ->
    gen_server:call(?MODULE, append_synched, FilePath, Text).

handle_call(append_synched, FilePath, Text, _From, LoopData) ->
    io:fwrite("Hello!~n", []),
    io:fwrite("LoopData:~p~n", LoopData),
    io:fwrite("~p~n", FilePath),
    io:fwrite("~p~n", Text).
    
handle_cast(append_asynched, Str) ->
    ok, theFile = file:write_file(theFile, Str).

stop() ->
 gen_server:cast(?MODULE, stop).

然后我使用 erlang shell 调用以下命令并获取错误:

1> cd("c:/Users/TalTe/WebAppsProjects/fileappender/src/").
c:/Users/TalTe/WebAppsProjects/fileappender/src
ok
2> c(appender_server).
ok,appender_server
3> appender_server:start_link("c:/temp/file.txt").
error,bad_return_value,"<0.88.0>"
4> =CRASH REPORT==== 2-Dec-2020::23:30:19.687000 ===
  crasher:
    initial call: appender_server:init/1
    pid: <0.87.0>
    registered_name: appender_server
    exception exit: bad_return_value,"<0.88.0>"
      in function  gen_server:init_it/6 (gen_server.erl, line 409)
    ancestors: [<0.79.0>]
    message_queue_len: 0
    messages: []
    links: [<0.79.0>]
    dictionary: []
    trap_exit: false
    status: running
    heap_size: 376
    stack_size: 28
    reductions: 257
  neighbours:
    neighbour:
      pid: <0.79.0>
      registered_name: []
      initial_call: erlang,apply,2
      current_function: io,execute_request,2
      ancestors: []
      message_queue_len: 0
      links: [<0.78.0>,<0.87.0>]
      trap_exit: false
      status: waiting
      heap_size: 1598
      stack_size: 26
      reductions: 5365
      current_stacktrace: [io,execute_request,2,[file,"io.erl",line,571],
                  shell,exprs,7,[file,"shell.erl",line,693],
                  shell,eval_exprs,7,[file,"shell.erl",line,642],
                  shell,eval_loop,3,[file,"shell.erl",line,627]]
** exception error: bad_return_value,"<0.88.0>"
4> appender_server:append_synched("c:/temp:/file.txt", "Hello").
** exception exit: noproc,gen_server,call,
                                       [appender_server,
                                        append_synched,"c:/temp:/file.txt","Hello"]
     in function  gen_server:call/2 (gen_server.erl, line 238)
5> 

请帮助我理解我在这里做错了什么? 顺便说一句,尽管第 3 步出错,但仍创建了文件。

【问题讨论】:

【参考方案1】:

init 回调应返回 ok, SomeState。在您的情况下,init 的返回值是 io_lib:print() 的返回值

【讨论】:

是的。你当然是对的。谢谢。尽管如此,最后一个命令还是崩溃了。我是否正确调用它:appender_server:append_synched("c:/temp/file.txt", "Hello")。 ?终止的共振: badarg,[io,format,[,"LoopData:~p~n",],[] 。异常错误:参数错误 应该是io:fwrite("LoopData:~p~n", [LoopData])。您还需要将其他呼叫更改为io:fwrite。此外,handle_call 的返回值不正确。应该是reply, SomeReply, SomeStatehandle_cast 的返回值也不正确。应该是noreply, SomeState

以上是关于Erlang:无法创建 gen_server:call()的主要内容,如果未能解决你的问题,请参考以下文章

Erlang:无法启动奴隶 - 错误,超时

Erlang:守护进程“init.d”脚本无法启动

为啥 Erlang 字符串不能用作 ets 表名?

Erlang/OTP 无法在 Solaris 11 11/11 上编译

简单的 Erlang 示例无法运行(错误)

无法通过 asdf (Ubuntu 18.04.1) 使用 openssl 安装 Erlang