API

Script loading functions

These functions are the most common entry points for loading LUA scripts on disk.

redis_lua.load_all_scripts(path, cache=None)

Load all the LUA scripts found at the specified location.

Parameters:
  • path – A path to search into for LUA scripts.
  • cache – A cache of scripts to use to fasten loading. If some names are in the cache, then they are taken from it.
Returns:

A list of scripts that were found, in arbitrary order.

redis_lua.load_scripts(names, path, cache=None)

Load several LUA scripts.

Parameters:
  • names – An iterable of LUA scripts names to load. If some names contain backslashes, those will be replaced with forward slashes silently.
  • path – A path to search into for LUA scripts.
  • cache – A cache of scripts to use to fasten loading. If some names are in the cache, then they are taken from it.
Returns:

A dict of scripts that were found.

Warning:

All scripts whose load succeeds are added in the cache immediately. This means that if some script fails to load and the function call throws, the cache will still have been updated.

redis_lua.load_script(name, path, cache=None, ancestors=None)

Load a LUA script.

Parameters:
  • name – The name of the LUA script to load, relative to the search path, without the ‘.lua’ extension. If the name contains backslashes, those will be replaced with forward slashes silently.
  • path – A path to search into for LUA scripts.
  • cache – A cache of scripts to use to fasten loading. If name is in the cache, then the result is the same as calling cache[name].
  • ancestors – A list of names to consider as ancestors scripts.
Returns:

A Script instance.

Script running functions

These functions are using to execute LUA code on Redis servers.

redis_lua.run_code(client, content, path=None, kwargs=None, cache=None)

Run a LUA script on the specified redis instance.

Parameters:
  • client – The Redis or pipeline instance to execute the script on.
  • content – The LUA code to execute.
  • path – The path to search for for scripts inclusion. Can be None, in which case, any included script must exist in the cache.
  • kwargs – A dict of arguments to call the script with.
  • cache – The script cache to use to fasten script inclusion.
Returns:

The return value, as given by the script.

Script instances

All loaded scripts are wrapped into a redis_lua.script.Script instance.

class redis_lua.script.Script(name, regions)
get_line_info(line)

Get the line information for the specified line.

Parameters:line – The line.
Returns:The (real_line, real_line_count, line, line_count, region) tuple or ValueError if no such line exists.
classmethod get_line_info_for_regions(regions, included_scripts)

Get a list of tuples (first_real_line, real_line, real_line_count, first_line, line, line_count, region) for the specified list of regions.

Params regions:A list of regions to get the line information from.
Params included_scripts:
 A set of scripts that were visited already.
Returns:A list of tuples.
get_real_line_content(line)

Get the real line content for the script at the specified line.

Parameters:line – The line.
Returns:A line content.
get_runner(client)

Get a runner for the script on the specified client.

Parameters:client – The Redis instance to call the script on.
Returns:The runner, a callable that takes the script named arguments and returns its result. If client is a pipeline, then the runner returns another callable, through which the resulting value must be passed to be parsed.
get_scripts_for_line(line)

Get the list of (script, line) by order of traversal for a given line.

Parameters:line – The line.
Returns:A list of (script, line) that got traversed by that line.
runner(client, **kwargs)

Call the script with its named arguments.

Returns:The script result.

Low-level script functions

These functions are useful for people that want to perform more advanced operations, such as parsing a Script file manually from another source than a file.

redis_lua.read_script(name, path, encoding=None)

Read a LUA script.

Parameters:
  • name – The name of the LUA script to load, relative to the search paths, without the ‘.lua’ extension. name may contain forward slash path separators to indicate that the script is to be found in a sub-directory.
  • path – A path to search into for LUA scripts.
  • encoding – The encoding to use to read the file. If none is specified, UTF-8 is assumed.
Returns:

The content of the script.

Raises:

If no such script is found, a ScriptNotFoundError is thrown.

redis_lua.parse_script(name, content, path=None, cache=None, ancestors=None)

Parse a LUA script.

Parameters:
  • name – The name of the LUA script to parse.
  • content – The content of the script.
  • path – The path of the script. Can be None if the script was loaded from memory. In this case, any included script must exist in the cache.
  • cache – A dict of scripts that were already parsed. The resulting script is added to the cache. If the currently parsed script exists in the cache, it will be overriden.
  • ancestors – A list of scripts that were called before this one. Used to detect infinite recursion.
Returns:

A Script instance.