Serialization Strategies¶
- class globus_compute_sdk.serialize.SerializationStrategy¶
A
SerializationStrategy
is in charge of converting function source code or arguments into string data and back again.- identifier: ClassVar[str]¶
The unique, 3-character string that identifies this strategy. The first two characters are the effective identifier; the third must always be the newline character
"\n"
.
- for_code: ClassVar[bool]¶
If
True
, this strategy works with callable Python objects - functions, classes that implement__call__
, etc. IfFalse
, this strategy works with all other Python objects (ie, arguments and keyword arguments in submissions). Note that a singleSerializationStrategy
cannot support both callables and non-callables.
- abstract serialize(data: Any) str ¶
Converts arbitrary Python data to a string representation.
- Parameters:
data – If
for_code
, any callable Python object; if notfor_code
, any other Python object. Serializers are not guaranteed to work with every callable or non-callable object.- Returns:
The string representation, which can be deserialized again by an instance of this strategy.
- Raises:
A
SerializationStrategy
may raise for various reasons if it cannot handle a given Python object.
- abstract deserialize(payload: str) Any ¶
Converts serialized string data back into the Python object it represents. Inverse of
serialize
.- Parameters:
payload – Serialized data generated by an instance of this strategy.
- Returns:
The Python object represented by the payload.
- Raises:
If the payload wasn’t serialized by an instance of this strategy, or if encountering other unexpected errors. May also raise if the payload was serialized with a different Python version than what is currently running.
Note
In the tables below, the ✅ symbol means that the given strategy supports the given
scenario, while the ❌ symbol means the given strategy cannot support the given
scenario. Note that even when the ✅ symbol is present, Python serialization is
tricky and there may be edge cases where the given strategy will fail; if in doubt,
test a strategy against a payload using check_strategies()
.
Warning
Some code serialization strategies make a best-effort attempt to work across Python version boundaries, as shown via the ⚠️ symbol. While they may work in many situations, there is no guarantee they will work in every situation or between every pair of versions. However, using the same Python version on the submit side and execution side is guaranteed to work, and is the recommended approach whenever possible.
- class globus_compute_sdk.serialize.CombinedCode¶
Bases:
SerializationStrategy
This strategy attempts to serialize the function via the
DillCodeSource
,DillCode
, andDillCodeTextInspect
strategies, combining the results of each sub-strategy as the final serialized payload. When deserializing, it tries each sub-strategy in order until one successfully deserializes its part of the payload. Intended for maximum compatibility, but is slightly slower and generates larger payloads than the other strategies.Supports¶ Functions defined in Python interpreter
Code from notebooks
Interop between mismatched Python versions
Decorated functions
✅
✅
⚠️
✅
- deserialize(payload: str, variation: int = 0) Any ¶
If a variation is specified, try that strategy/payload if present. If variation is not specified, loop and return the first one that deserializes successfully. If none works, raise
Note variation is 1-indexed (ie. 2 means 2nd strategy), not the
identifier
of the strategy (like"04\n"
,"01\n"
).
- class globus_compute_sdk.serialize.DillCode¶
Bases:
SerializationStrategy
Directly serializes the function via
dill.dumps()
, then encodes it in base 64.Supports¶ Functions defined in Python interpreter
Code from notebooks
Interop between mismatched Python versions
Decorated functions
❌
✅
❌
✅
- class globus_compute_sdk.serialize.DillCodeSource¶
Bases:
SerializationStrategy
Extracts a function’s body via
dill.source.getsource()
, serializes it withdill.dumps()
, then encodes it via base 64.Supports¶ Functions defined in Python interpreter
Code from notebooks
Interop between mismatched Python versions
Decorated functions
✅
❌
⚠️
❌
- class globus_compute_sdk.serialize.DillCodeTextInspect¶
Bases:
SerializationStrategy
Extracts a function’s body via
inspect.getsource()
, serializes it withdill.dumps()
, then encodes it via base 64.Supports¶ Functions defined in Python interpreter
Code from notebooks
Interop between mismatched Python versions
Decorated functions
❌
✅
⚠️
❌
- class globus_compute_sdk.serialize.PureSourceDill¶
Bases:
SerializationStrategy
Extracts a function’s definition via
dill.source.getsource()
and sends the resulting string directly over the wire.Supports¶ Functions defined in Python interpreter
Code from notebooks
Interop between mismatched Python versions
Decorated functions
✅
❌
⚠️
❌
- identifier: ClassVar[str] = 'sd\n'¶
The unique, 3-character string that identifies this strategy. The first two characters are the effective identifier; the third must always be the newline character
"\n"
.
- for_code: ClassVar[bool] = True¶
If
True
, this strategy works with callable Python objects - functions, classes that implement__call__
, etc. IfFalse
, this strategy works with all other Python objects (ie, arguments and keyword arguments in submissions). Note that a singleSerializationStrategy
cannot support both callables and non-callables.
- class globus_compute_sdk.serialize.PureSourceTextInspect¶
Bases:
SerializationStrategy
Extracts a function’s definition via
inspect.getsource()
and sends the resulting string directly over the wire.Supports¶ Functions defined in Python interpreter
Code from notebooks
Interop between mismatched Python versions
Decorated functions
❌
✅
⚠️
❌
- identifier: ClassVar[str] = 'st\n'¶
The unique, 3-character string that identifies this strategy. The first two characters are the effective identifier; the third must always be the newline character
"\n"
.
- for_code: ClassVar[bool] = True¶
If
True
, this strategy works with callable Python objects - functions, classes that implement__call__
, etc. IfFalse
, this strategy works with all other Python objects (ie, arguments and keyword arguments in submissions). Note that a singleSerializationStrategy
cannot support both callables and non-callables.
- class globus_compute_sdk.serialize.DillDataBase64¶
Bases:
SerializationStrategy
Serializes Python data to binary via
dill.dumps()
, and then encodes that binary to a string using base 64 representation.
- class globus_compute_sdk.serialize.JSONData¶
Bases:
SerializationStrategy
Serializes Python data to a string via
json.dumps()
.