-
-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add better support for LUA scripts #304
Comments
Did you install fakeredis with lua support? |
Yes |
I checked, to load I might add an environment variable |
So from what you say, to avoid this error I have to compile it on the machine that is going to run it with the environment variable LUA_MODULES=cjson right? Or could this be a bug in the lua script? local ruleset = cjson.decode(ARGV[1])
-- Set limits
for i, key in pairs(KEYS) do
redis.call('SET', key, ruleset[key][1], 'EX', ruleset[key][2], 'NX')
end
-- Check limits
for i = 1, #KEYS do
local value = redis.call('GET', KEYS[i])
if value and tonumber(value) < 1 then
return ruleset[KEYS[i]][2]
end
end
-- Decrease limits
for i, key in pairs(KEYS) do
redis.call('DECR', key)
end
return 0 |
Well, I would need to implement this, but yeah, eventually, that would be the way it works. |
I use poetry to manage dependencies, so when I get a whl from pypi I will download that one. I understand that in that case I will not use anything from LUA_MODULES because I will download it already compiled. |
does cjson have a pypi package? |
It seems that there are packages like ujson that give this functionality (I have not tested them). |
We need something where this would run: import lupa
lupa.LuaRuntime().execute("require('cjson')") I don't think ujson meets this requirement |
Trying with this code I get this error: lupa.lua54.LuaError: [string "<python>"]:1: module 'cjson' not found:
no field package.preload['cjson']
no file 'C:\Users\jcmencia\AppData\Local\Programs\Python\Python312\lua\cjson.lua'
no file 'C:\Users\jcmencia\AppData\Local\Programs\Python\Python312\lua\cjson\init.lua'
no file 'C:\Users\jcmencia\AppData\Local\Programs\Python\Python312\cjson.lua'
no file 'C:\Users\jcmencia\AppData\Local\Programs\Python\Python312\cjson\init.lua'
no file 'C:\Users\jcmencia\AppData\Local\Programs\Python\Python312\..\share\lua\5.4\cjson.lua'
no file 'C:\Users\jcmencia\AppData\Local\Programs\Python\Python312\..\share\lua\5.4\cjson\init.lua'
no file '.\cjson.lua'
no file '.\cjson\init.lua'
no file 'C:\Users\jcmencia\AppData\Local\Programs\Python\Python312\cjson.dll'
no file 'C:\Users\jcmencia\AppData\Local\Programs\Python\Python312\..\lib\lua\5.4\cjson.dll'
no file 'C:\Users\jcmencia\AppData\Local\Programs\Python\Python312\loadall.dll'
no file '.\cjson.dll'
stack traceback:
[string "<python>"]:1: in main chunk
[C]: in function 'require' A little closer to making it work we are! |
This is probably something that should be implemented in the lupa package. |
lupa supports importing modules, it simply does not have cjson ootb. I managed to install cjson:
|
I have been researching and there is a "cleaner" way to do this: import lupa
lupa.LuaRuntime().require("cjson") |
Does it work without installing cjson first? |
No, as you said it seems that lupa does not bring the cjson module with it. This makes more "complex", in my case, mock redis for asgi-ratelimit since I want for my CI to use python base only, and it doesn't seem right to upload cjson.dll or cjson.so to the repository either. |
yeah, I am not sure why asgi-ratelimit chose to implement a ratelimit using a script that uses cjson - it seems a bit cumbersome. |
@cunla Hi, I have the same issue, and I don't understand what the solution is - where should I add this code |
This is the error I'm getting:
|
Shalom Dvora, Here is an explanation how to use it in fakeredis. The gist: import fakeredis
r = fakeredis.FakeStrictRedis(lua_modules={"my_module.so"}) In terms of where to put cjson.so - You have in the stacktrace where LUA is looking for it. I recommend you add it to the project's working directory ( |
Thanks @cunla for the quick response!
Am I doing something wrong? |
your code seems right. How do you run the tests? what is the working directory relative to where is cjson.so? say the structure is:
then if you are executing the test from |
I run from tests directory, with pytest |
... I am pretty sure the working directory you are running from is incorrect. Try this: Add to your test:
import lupa
with lupa.allow_lua_module_loading()
from lupa import lua54
lua = lua51.LuaRuntime()
lua.require('cjson') If this gives the same error (and the file is there), it may be corrupted, or compiled for the wrong LuaRuntime version. |
@cunla The working directory is correct, the cjson.so file is in the list.
And thanks again. |
Up to you what you are trying to do. Redis is using 5.1. You need to use the same version inside the |
Is your feature request related to a problem? Please describe.
I am trying to mock the redi backend for asgi-ratelimit and I am encountering the following error:
Describe the solution you'd like
Add support for cjson? I don't know if this is only the problem.
I don't know exactly if this would be for lupa or for fakeredis, if you can give me some guidance I would appreciate it.
Related issues:
Additional context
I have tried this code:
Upvote & Fund
The text was updated successfully, but these errors were encountered: