3

I trying to execute this lua script i get proper output too. But i keep getting Wrong number of args calling Redis command From Lua script

def new_get_following(self, start, count, user_id=0):
        script = """
        local envs = redis.call('zrevrange',KEYS[1],ARGV[3],ARGV[4]);
        redis.call('sadd',ARGV[1],unpack(envs));
        local favs = redis.call('sinter',ARGV[2],ARGV[1]);
        local acts= redis.call('mget',unpack(envs));
        redis.call('del',ARGV[1]);
        return {favs,envs,acts}
        """
        count = int(start) + int(count) - 1
        print count
        fav_key = self.fav_key + ":" + str(user_id)
        following_stream_key = self.following_stream_key + ":" + str(user_id)
        tmp_key = int(time.time())
        return self.exectute(script, args=[tmp_key, fav_key, start, count], keys=[following_stream_key])
1
  • 1
    Use the MONITOR command (from redis-cli) to understand what is exactly sent to Redis by your application. Commented Jun 23, 2015 at 16:40

2 Answers 2

2

Maybe it is just a typo and has already been corrected but:

self.exectute shouldn't it be self.execute?

Sign up to request clarification or add additional context in comments.

Comments

1

In the code last line is causing error.

local envs = redis.call('zrevrange',KEYS[1],ARGV[3],ARGV[4]);
local acts= redis.call('mget',unpack(envs));

As if envs is empty table then second line:

local acts= redis.call('mget',unpack(envs));

becomes this:

local acts= redis.call('mget',unpack());

so lua keeps throwing error. For avoid this error we can use redis.pacall which gives Response error object which can be checked in output can handle error. So final code should be

def new_get_following(self, start, count, user_id=0):
        script = """
        local envs = redis.call('zrevrange',KEYS[1],ARGV[3],ARGV[4]);
        redis.call('sadd',ARGV[1],unpack(envs));
        local favs = redis.call('sinter',ARGV[2],ARGV[1]);
        local acts= redis.pcall('mget',unpack(envs));
        redis.call('del',ARGV[1]);
        return {favs,envs,acts}
        """
        count = int(start) + int(count) - 1
        print count
        fav_key = self.fav_key + ":" + str(user_id)
        following_stream_key = self.following_stream_key + ":" + str(user_id)
        tmp_key = int(time.time())
        return self.exectute(script, args=[tmp_key, fav_key, start, count], keys=[following_stream_key])

1 Comment

That seems wrong. acts would be true or false, whether the pcall succeeded or not. So you are not getting the results from mget anywhere. Why don't you test envs length first with #envs and if > 0 then you do the mget thing ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.