我正在使用队列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),
我不知道我做错了什么。
答案 0 :(得分:6)
您拥有的内容([{[],[]}]
)是列表中的队列。标准队列看起来像{list(), list()}
。显然,正因如此,每次对队列的调用都会失败。
我的猜测是你要么把你的队列弄错了,要么以错误的方式提取它。
答案 1 :(得分:2)
首先,你得到的是一个“badarg”错误。从Erlang队列的文档中读取:
所有功能都因为badarg而失败 如果参数是错误的类型,为 示例队列参数不是 队列,索引不是整数,列表 参数不是列表。不当 列表导致内部崩溃。一个索引 超出队列的范围也会导致 失败的原因是badarg。
在您的情况下,您传递给队列的内容: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),