Erlang" badarg",不确定如何处理

时间:2016-10-26 17:42:54

标签: erlang

在开始之前,我已经检查过Handle badarg in Erlang 但是我在未定义的检查中仍然没有成功,所以我将它们删除了。

我正在建立一个虚拟银行流程,当客户端对银行流程进行余额查询检查时,程序退出,说:

Error in process <0.373.0> with exit value:
{badarg,[{project4,client,3,
                   [{file,"/Users/owner/Desktop/bank.erl"},
                    {line,27}]}]}

=ERROR REPORT==== 26-Oct-2016::13:34:57 ===
Error in process <0.379.0> with exit value:
{badarg,[{project4,client,3,
                   [{file,"/Users/owner/Desktop/bank.erl"},
                    {line,27}]}]}

=ERROR REPORT==== 26-Oct-2016::13:34:57 ===
Error in process <0.375.0> with exit value:
{badarg,[{project4,client,3,
                   [{file,"/Users/owner/Desktop/bank.erl"},
                    {line,27}]}]}

=ERROR REPORT==== 26-Oct-2016::13:34:58 ===
Error in process <0.377.0> with exit value:
{badarg,[{project4,client,3,
                   [{file,"/Users/owner/Desktop/bank.erl"},
                    {line,27}]}]}
<0.378.0> Balance request: 54> 
=ERROR REPORT==== 26-Oct-2016::13:34:58 ===
Error in process <0.378.0> with exit value:
{badarg,[{project4,client,3,
                   [{file,"/Users/owner/Desktop/bank.erl"},
                    {line,39}]}]}
<0.372.0> Balance request: 54> 
=ERROR REPORT==== 26-Oct-2016::13:34:58 ===
Error in process <0.372.0> with exit value:
{badarg,[{project4,client,3,
                   [{file,"/Users/owner/Desktop/bank.erl"},
                    {line,39}]}]}

模拟代码在这里:

-module(bankSim).
-export([start/0, negativeOrPositive/0, sleep/1, generate_rand_int_list/3, generate_rand/2, client/3, clientSpawn/2, bank/2]).

negativeOrPositive() -> 
    M = rand:uniform(),
    if
        M =< 0.5 -> 
            1;
        true -> 
            -1
    end.

sleep(T) ->
    receive
        after T -> ok
    end.

generate_rand_int_list(N,StartVal,Lim) ->
    lists:map(fun (_) -> (rand:uniform(Lim-StartVal) + StartVal)* negativeOrPositive() end, lists:seq(1,N)).

generate_rand(StartVal, Lim) ->
    rand:uniform(Lim-StartVal) + StartVal.

client([], _ , BankID) ->
    BankID ! {goodbye};
client([H|T], Count, BankID) ->
    BankID ! {transaction, self(), H},
    receive
        {Amount, Balance, Success} ->
            io:format("Client: ~w, Amount requested: ~w, Bank Balance: ~w, Transaction successful ~w ~n",[self(), Amount, Balance, Success]);
        { X } ->
            io:format("The balance is ~w ~n", [X])
    end,    
    sleep(generate_rand(500, 1500)),
    Mod = Count rem 5,
    if 
        Mod == 0 -> 
            io:format("~w Balance request: ",[ self() ]),
            BankID! {balance, self()};
        true -> 
            ok
    end,
    client(T, Count + 1, BankID).

clientSpawn(0, _) ->
        io:format("Finished spawning clients ~n",[]);
clientSpawn(N, BankID) -> 
        spawn(bankSim, client, [ generate_rand_int_list( generate_rand(10, 20), 0, 100) , 1, BankID] ),
        clientSpawn(N-1, BankID).


bank(Balance, 0) ->
    io:format("Banking simulation ended with a final balance of ~w ~n", [Balance]),
    io:format("simulation completed ~n", []);
bank(Balance, NumClients) ->
    receive
        {balance, Client} ->
            Client ! {Balance};
        {transaction, Client, Amount} ->
            NewBalance = Balance + Amount,
            if 
                NewBalance =< 0 ->
                    Client ! {Amount, NewBalance, no},
                    bank(Balance, NumClients);
                NewBalance > 0 ->
                    Client ! {Amount, NewBalance, yes},
                    bank(NewBalance, NumClients);
                true -> 
                    io:format("This will never be printed")
            end;
        goodbye ->
            NewNumClients = NumClients - 1,
            bank(Balance, NewNumClients)
    end.


start()->
    N = generate_rand(2,10),
    register(bank ,spawn(bankSim, bank, [generate_rand(2000,3000), N])),
    clientSpawn(N,bank).

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:7)

Erlang文档会告诉您badarg表示"The argument is of wrong data type, or is otherwise badly formed."这是我从未发现的非常明确的内容。

Dogbert指出,在您的某个案件中,您错过了对bank/2的递归通话。这是badarg的来源 - 但为什么?

我总是认为badarg意思是“发送给BIF的不良论点”。如果您正在调用常规的Erlang函数,则会出现function_clause错误,这意味着没有与您的数据匹配的函数子句。

您收到的错误消息列出了两个不同的行号。一个是行

BankID ! {transaction, self(), H},

的另一个
BankID! {balance, self()};

不好的论点是BankID。它之所以糟糕是因为它是一个原子,bank。在start/0函数中,您生成一个新的银行流程,然后将该流程的PID注册为bank。然后在您的客户端中,您将消息发送到bank原子并让Erlang将其解析为已注册的PID。

它可以工作一段时间,但是当你遇到丢失 - 重复的情况时,你的bank进程就会终止。现在没有人在bank原子后面注册,并且send运算符因为无法发送到已解散的名称而爆炸。

如果您将代码更改为

Bank = spawn(bankSim, bank, [generate_rand(2000,3000), N]),
clientSpawn(N, Bank).

然后你不会得到badarg,因为将消息发送到没有人正在运行的PID并不是一个错误。您可能喜欢在不修复bank循环并查看会发生什么的情况下尝试这样做。