MCPcopy Create free account
hub / github.com/cloudpipe/cloudpickle / register_pickle_by_value

Function register_pickle_by_value

cloudpickle/cloudpickle.py:127–163  ·  view source on GitHub ↗

Register a module to make its functions and classes picklable by value. By default, functions and classes that are attributes of an importable module are to be pickled by reference, that is relying on re-importing the attribute from the module at load time. If `register_pickle_by_v

(module)

Source from the content-addressed store, hash-verified

125
126
127def register_pickle_by_value(module):
128 """Register a module to make its functions and classes picklable by value.
129
130 By default, functions and classes that are attributes of an importable
131 module are to be pickled by reference, that is relying on re-importing
132 the attribute from the module at load time.
133
134 If `register_pickle_by_value(module)` is called, all its functions and
135 classes are subsequently to be pickled by value, meaning that they can
136 be loaded in Python processes where the module is not importable.
137
138 This is especially useful when developing a module in a distributed
139 execution environment: restarting the client Python process with the new
140 source code is enough: there is no need to re-install the new version
141 of the module on all the worker nodes nor to restart the workers.
142
143 Note: this feature is considered experimental. See the cloudpickle
144 README.md file for more details and limitations.
145 """
146 if not isinstance(module, types.ModuleType):
147 raise ValueError(f"Input should be a module object, got {str(module)} instead")
148 # In the future, cloudpickle may need a way to access any module registered
149 # for pickling by value in order to introspect relative imports inside
150 # functions pickled by value. (see
151 # https://github.com/cloudpipe/cloudpickle/pull/417#issuecomment-873684633).
152 # This access can be ensured by checking that module is present in
153 # sys.modules at registering time and assuming that it will still be in
154 # there when accessed during pickling. Another alternative would be to
155 # store a weakref to the module. Even though cloudpickle does not implement
156 # this introspection yet, in order to avoid a possible breaking change
157 # later, we still enforce the presence of module inside sys.modules.
158 if module.__name__ not in sys.modules:
159 raise ValueError(
160 f"{module} was not imported correctly, have you used an "
161 "`import` statement to access it?"
162 )
163 _PICKLE_BY_VALUE_MODULES.add(module.__name__)
164
165
166def unregister_pickle_by_value(module):

Calls

no outgoing calls