2010-12-04 60 views
2

我正在使用队列api,并遇到错误使我的程序崩溃。Erlang队列问题

首先我取从字典它返回这个在打印输出

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

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

然后,无论是当我尝试添加到队列或获得其长度,我得到一个错误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,[{队列,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的呼叫:ha ndle_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), 

我不知道我做错了。

回答

6

你有什么([{[],[]}])是一个列表中的队列。标准队列看起来像{list(), list()}。显然,正因为如此,随后每次对队列的调用都会失败。

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

+0

非常感谢。它现在看起来很清楚我做错了什么,我认为字典的价值必须在[]内。我不知道我为什么这样做。我一遍又一遍地检查了我的代码,忽略了这一点。我无法感谢你 – jarryd 2010-12-04 15:12:17

2

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

所有功能会失败,原因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), 
+0

非常感谢你对这两个问题的帮助。所以我正确地传递了状态。我不知道为什么我认为价值必须是[价值]。 :) – jarryd 2010-12-04 15:19:17