Using python, how to return multiple values from a lua script

1,766 views
Skip to first unread message

David Montgomery

unread,
Jul 12, 2012, 9:52:57 AM7/12/12
to redi...@googlegroups.com
Hi,

I am trying to return multiple values from a lua script.

In this test I set up two keys as follows
hmset a b c
hmset c d e

I want to use lua to return the value of 'c' given key a and b and
to get all keys for c i.e, a python dictionary. The issue is that I
cant return both values, only one or the other.

e.g. if return ckid, meta then returns ckid

if return meta, ckid then returns meta.

I cant return both meta and ckid. From my understanding of lua,
returns are like python e.g. return a,b

I am using kickass redis

import redis
from kickass_redis.patterns.lua import LuaCall, LuaScriptError

r = redis.StrictRedis()
match = """
local ckid = redis.pcall('hget',KEYS[1],ARGV[1])
local meta = redis.call('hgetall', ckid)
return ckid, meta
"""
mult = LuaCall(match, r)
f,g = mult(keys = ('a',), args = ('b',))

Thanks

Dvir Volk

unread,
Jul 12, 2012, 10:02:48 AM7/12/12
to redi...@googlegroups.com
Hi David,
I tried (both in Kickass and directly in the console) to return the values as a list, and it seems to work well. won't that work for you?
just do

    return { ckid, meta }

and you'll get a list back, which you can unpack.


--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To post to this group, send email to redi...@googlegroups.com.
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.


David Montgomery

unread,
Jul 12, 2012, 10:09:58 AM7/12/12
to redi...@googlegroups.com
Ahhh....you did { ckid, meta } rather than return ckid, meta? yes
....that worked! thanks

David Montgomery

unread,
Jul 12, 2012, 10:17:22 AM7/12/12
to redi...@googlegroups.com
PS,

This is the return...the second item should be a dictionary {'d': 'e'}
e.g. r.hgetall('c')={'d': 'e'}

when I ran the code I got:
['c', ['d', 'e']]

Thanks

On Thu, Jul 12, 2012 at 10:09 PM, David Montgomery

Dvir Volk

unread,
Jul 12, 2012, 11:14:22 AM7/12/12
to redi...@googlegroups.com
The client doesn't have any information on how to format the return. Redis' response is always returned as nested lists to python, and it's redis-py's job to turn those into dicts, sets, etc.
now, in the case of lua, without peeking into the code, it's impossible to know what the return type should be.

what I can do is add to LuaCall, some sort of way to tell the call what return types it should pack.
something like:

mult = LuaCall(match, conn, returns=[string,dict])

and then the function will auto convert the return. If that's interesting to you, we can continue the discussion on the kickass redis mailing list, as it's a bit off topic here :)
Reply all
Reply to author
Forward
0 new messages