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