在使用ngx.thread,redis:lpop,http请求时,遇到lpop错乱或者取不出来值的问题

45 views
Skip to first unread message

正好的温度

unread,
Feb 14, 2017, 1:55:41 AM2/14/17
to openresty
queue.lua


_product = {}
_product['default'] = {
redo_num = 3,
redo_time = 300,
do_num = 10,
}

_product['ami-weixin'] = {
redo_num = 3,
redo_time = 10,
do_num = 10,
}
--
local function dolist(v)
    return _queue_do:index(v)
end

for k,v in pairs(_product) do
    v['productId'] = k
    threads[k] = ngx.thread.spawn(dolist, v)
end

for k,v in pairs(threads) do
    ok,waits[k] = ngx.thread.wait(v)
end



queue_do.lua
function queue_do:index(con)

    local num = con.do_num

    ngx.ctx._redis = nil
    ngx.ctx._redis = redis:new(nil,ngx.ctx._config._sc_conf_redis)

    local key = _string.key_head..con['productId']

    local restable = {}
    for i=1,num do
        local res,err = ngx.ctx._redis:lpop(key)

        if not _fun:value_true_false(res) then
            break
        end

        res = json.decode(res)
        restable[i] = res
    end

    ---把取出来的任务,使用thread全部http请求
    ---ngx.thread.spawn(http请求)
    ---

end


会有计划任务每秒调用一次queue ,queue再会根据配置调用queue_do的index方法,index:lpop预先放好的队列,然后同时http请求执行;

出现的问题:_product配置里有两条时,会出现lpop出来的结果错乱,比如第一个配置里取的值到第二个键值tab里(restable);
            同时还会出现:循环10次lpop,但只出来2个值,里面还有值,但在这次运行中取不出来,在下次运行中又取出来了;
            但当_product配置只有一个配置时,结果运行的都正常;
比较诡异,这个里面可能问题点就在thread,socket的使用问题,但没找到问题点,请各位帮忙看看












Reply all
Reply to author
Forward
0 new messages