【问题标题】:Erlang queue problemsErlang 队列问题
【发布时间】:2010-12-04 23:10:04
【问题描述】:

我正在使用队列 api,但遇到了导致程序崩溃的错误。

首先,我从字典中获取队列,并在打印输出中返回此队列

获取的队列是[{[],[]}]

这正常吗?队列创建是否正确?

然后,当我尝试添加到队列或获取其长度时,我都会收到错误 badargs 。

TorrentDownloadQueue = dict:fetch(torrentDownloadQueue, State),
io:format("The fetched queue is  ~p~n", [dict:fetch(torrentDownloadQueue, State)]),
% Add the item to the front of the queue (main torrent upload queue)
TorrentDownloadQueue2 = queue:in_r(Time, TorrentDownloadQueue),
% Get the lenght of the downloadQueue
TorrentDownloadQueueLength = queue:len(TorrentDownloadQueue2),

当我尝试插入值 10 时,错误是

** 终止原因 == ** {badarg,[{queue,in_r,[10,[{[],[]}]]}, {ph_speed_calculator,handle_cast,2}, {gen_server,handle_msg,5}, {proc_lib,init_p_do_apply,3}]} ** 异常退出:badarg 在函数队列中:in_r/2 称为队列:in_r(10,[{[],[]}]) 来自 ph_speed_calculator:handle_cast/2 的调用 来自 gen_server:handle_msg/5 的调用 从 proc_lib:init_p_do_apply/3 调用 13>

这是 in_r 的错误,但我也收到了 len 调用的 badargs 错误。

我调用这些的方式有什么问题,或者初始队列不正确? 我按如下方式创建队列并将其添加到字典中

TorrentDownloadQueue = queue:new(),
TorrentUploadQueue = queue:new(),
D4 = dict:store(torrentDownloadQueue, [TorrentDownloadQueue], D3),
D5 = dict:store(torrentUploadQueue, [TorrentUploadQueue], D4),

我不知道我做错了什么。

【问题讨论】:

    标签: erlang queue


    【解决方案1】:

    您所拥有的 ([{[],[]}]) 是列表中的一个队列。标准队列类似于{list(), list()}。显然,正因为如此,后续对队列的每次调用都会失败。

    我的猜测是你要么携带错误队列,要么以错误的方式提取队列。

    【讨论】:

    • 非常感谢。现在看起来很清楚我做错了什么,我认为字典的值必须在 [] 内。我不知道我为什么这样做。我一遍又一遍地检查我的代码,忽略了这一点。感激不尽
    【解决方案2】:

    首先,你得到的是一个“badarg”错误。阅读 Erlang 队列的文档:

    所有函数都失败,原因是 badarg 如果参数类型错误,则为 示例队列参数不是 队列,索引不是整数,列表 参数不是列表。不当 列表会导致内部崩溃。一个索引 超出队列范围也会导致 失败原因是 badarg。

    在您的情况下,您传递给 queue:in_r 的不是队列,而是包含队列的列表。

    当你这样做时,你正在向字典中添加一个包含队列的列表:

    D4 = dict:store(torrentDownloadQueue, [TorrentDownloadQueue], D3),
    D5 = dict:store(torrentUploadQueue, [TorrentUploadQueue], D4),
    

    相反,你应该这样做:

    D4 = dict:store(torrentDownloadQueue, TorrentDownloadQueue, D3),
    D5 = dict:store(torrentUploadQueue, TorrentUploadQueue, D4),
    

    【讨论】:

    • 非常感谢您对这两个问题的帮助。所以我正确地传递了状态。我不知道为什么我认为价值必须是[价值]。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多