RedisGears Functions Runtime ¶
Python RedisGears functions are run using an embedded Python interpreter. Each function uses a separate sub-interpreter. All functions share the same environment and dependencies. The environment is imported with several defaults.
The following sections describe the runtime environment.
Python Interpreter ¶
RedisGears embeds a Python version 3.7.2+ interpreter.
All functions use that interpreter. Each call to
RG.PYEXECUTE
maintains its own globals dictionary that isolates its execution context from other calls. This means that all the functions submitted in that call share the interpreter and globals dictionary.
Environment ¶
The interpreter's environment can be extended with any dependent package that can later be imported and used by functions in their respective sub-interpreters.
Further reference
For more information about installing dependencies refer to:
GearsBuilder ¶
The
GearsBuilder
class is imported to the runtime's environment by default.
It exposes the functionality of the function's context builder .
execute ¶
The
execute()
function is imported to the runtime's environment by default.
This function executes an arbitrary Redis command.
Python API
def execute(command, *args)
Arguments
- command : the command to execute
- args : the command's arguments
Examples
# Pings the server (reply should be 'PONG')
reply = execute('PING')
atomic ¶
The
atomic()
Python context is imported to the runtime's environment by default.
The context ensures that all operations in it are executed atomically by blocking the main Redis process.
Python API
class atomic()
Examples
# Increments two keys atomically
def transaction(_):
with atomic():
execute('INCR', f'{{{hashtag()}}}:foo')
execute('INCR', f'{{{hashtag()}}}:bar')
gb = GB('ShardsIDReader')
gb.foreach(transaction)
gb.run()
configGet ¶
The
configGet()
function is imported to the runtime's environment by default.
This function fetches the current value of a RedisGears configuration option.
Python API
def configGet(key)
Arguments
- key : the configuration option key
Examples
# Gets the current value for 'ProfileExecutions'
foo = configGet('ProfileExecutions')
gearsConfigGet ¶
The
gearsConfigGet()
function is imported to the runtime's environment by default.
This function fetches the current value of a RedisGears configuration option and returns a default value if that key does not exist.
Python API
def gearsConfigGet(key, default=None)
Arguments
- key : the configuration option key
- default : a default value
Examples
# Gets the 'foo' configuration option key and defaults to 'bar'
foo = gearsConfigGet('foo', default='bar')
hashtag ¶
The
hashtag()
function is imported to the runtime's environment by default.
This function returns a hashtag that maps to the lowest hash slot served by the local engine's shard. Put differently, it is useful as a hashtag for partitioning in a cluster.
Python API
def hashtag()
Examples
# Get the shard's hashtag
ht = hashtag()
log ¶
The
log()
function is imported to the runtime's environment by default.
This function prints a message to Redis' log.
Python API
def log(message, level='notice')
Arguments
- message : the message to output
-
level
: the message's log level can be one of these:
- 'debug'
- 'verbose'
- 'notice'
- 'warning'
Examples
# Dumps every datum in the DB to the log for "debug" purposes
GB().foreach(lambda x: Log(str(x), level='debug')).run()