我正在使用队列 api 并且遇到了导致我的程序崩溃的错误。
首先我从字典中获取队列并在打印输出中返回它
The fetched queue is [{[],[]}]
这正常吗?队列创建是否正确?
然后,当我尝试添加到队列或获取其长度时,我都会在两者上收到错误 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 时,错误是
** Reason for termination == ** {badarg,[{queue,in_r,[10,[{[],[]}]]}, {ph_speed_calculator,handle_cast,2}, {gen_server,handle_msg,5}, {proc_lib,init_p_do_apply,3}]} ** exception exit: badarg in function queue:in_r/2 called as queue:in_r(10,[{[],[]}]) in call from ph_speed_calculator:handle_cast/2 in call from gen_server:handle_msg/5 in call from 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),
我不知道我做错了什么。
最佳答案
您拥有的 ([{[],[]}]
) 是列表中的一个队列。标准队列看起来像 {list(), list()}
。显然,正因为如此,后续对队列的每次调用都会失败。
我的猜测是,您要么携带错误的队列,要么以错误的方式提取它。
关于Erlang队列问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4353461/