Advanced Concepts

This section discuss some more advanced topics, considerations an usage patterns.

Redgrease Extras Options

It is always recommended to install either the redgrease package with either redgrease[client], redgrease[cli] or redgrease[all] package options on your clients.

However for the RedisGears server runtime, you may want to be more prudent with what you install. Therefore, RedGrease strives to give you visibility and control of control of how much of RedGrease you want to install on on your RedisGear server.

For the server you may want to consider these different options:

  • redgrease[runtime]

    This is the default and recommended package is to install on your server as this gives you access to every runtime feature in RedGrease, including all the GearFunction constructs, the Serverside Redis Commands, as well as the Builtin Runtime Functions.

    This is what Gears.pyexecute() is going to automatically assume for you when you pass any dynamic GearFunction to it, or if you set the enforce_redgrease argument to either True or just a version string (e.g. "0.3.12").

    This option installs all the dependencies that are needed for any of the RedGrease runtime features, but none of the dependencies that are only required for the RedGrease client features.

  • redgrease

    You can also install just the bare redgrease package without any dependencies. This limits the RedGrease functionalities that you can use to the ones provided by the “Clean” RedGrease Modules.

    Most notably this will prevent you from using the Serverside Redis Commands.

    To enforce this option, ensure that any calls to Gears.pyexecute() explicitly set the enforce_redgrease argument to "redgrease" (without extras). Version qualifiers are supported (e.g. "redgrease>0.3").

  • Nothing

    Yes, In some cases RedGrease may be of value even if it is not installed in the RedisGears runtime environment. If your are only executing GearFunctions by Script File Path, and the script itself is either:

    A. Not importing redgrease at all (obviously), or

    B. Only using explicitly imported Builtin Runtime Functions, I.e. only importing RedGrease with:

    from redgrease.runtime import ...
    

    If this is the case, you can enforce that RedGrease is not added to the runtime requirements at all by ensuring that any calls to Gears.pyexecute() explicitly set the enforce_redgrease argument to False. This option will not add any redgrease requirement to the function and simply ignore any explicit runtime imports.

    Note

    This only applies to explicit imports of symbols in the runtime module, and not to imports of the module itself.

    I.e, imports of the form:

    from redgrease.runtime import GB, hashtag
    

    Or:

    from redgrease.runtime import *
    

    But not:

    import redgrease.runtime
    

    Nor:

    from redgrease import GB, hashtag
    

Dependency Packages per Option

The dependencies of the different extras options are as follows:

  • redgrease

  • redgrease[runtime]

    • attrs - This dependency may be removed in future versions.

    • cloudpickle - This dependency may be replaced with dill in future versions.

    • redis

    • packaging - This dependency may be moved to the client extra in future versions.

    • wrapt - This dependency may be removed in future versions.

  • redgrease[client]

  • redgrease[all]

    • All dependencies above

“Clean” RedGrease Modules

The “clean” RedGrease modules, that can be used without extra dependencies are:

  • redgrease.runtime - Wrapped versions of the built-in runtime functions, but with docstrings and type hints.

  • redgrease.reader - GearFunction constructors for the various Reader types.

  • redgrease.func - Function decorator for creating CommandReader functions.

  • redgrease.utils - A bunch of helper functions.

  • redgrease.sugar - Some trivial sugar for magic strings and such.

  • redgrease.typing - A bunch of type helpers, typically not needed to be imported in application code.

  • redgrease.gears - The core internals of RedGrease, rarely needed to be imported in application code.

  • redgrease.hysteresis - A helper module, specifically for the RedGrease CLI. Not intended to be imported in application code.

Python 3.6 and 3.8+

Dynamically created GearFunction objects can only be executed if the client Python version match (major and minor version) the Python version of the RedisGears runtime. At the moment of writing, RedisGears version 1.0.6, is relying on Python 3.7.

RedGrease does however support using any Python version after Python 3.6 inclusive, for all other functionalities.

You are still able to construct an run Gear functions using the RedGrease GearFunction objects, but only if executed using Script File Path.

This means that you need to:

  1. Put your Gear Function code in a separate file from your application code.

  2. Ensure that the Gear Function script, only use Python 3.7 constructs.

  3. Execute the function by passing the script file path to redgrease.client.Gears.pyexecute().

Legacy Gear Scripts

You do not have to change any of your existing legacy scripts to start using RedGrease.

RedGrease support running “vanilla” RedisGears Gear functions, i.e. without any RedGrease features, by execution using Script File Path.

If you however need to modify any of your legacy scripts, it may be a good idea to add from redgrease import execute, atomic, configGet, gearsConfigGet, hashtag, log, GearsBuilder, GB to the import section of your script so that you get the benefits of autocompletion and write-time type checking (assuming your IDE supports it).


Courtesy of : Lyngon Pte. Ltd.