Serialize the state for redis. Returns: The serialized state. Raises: StateSerializationError: If the state cannot be serialized. # noqa: DAR401: e # noqa: DAR402: StateSerializationError
(self)
| 2141 | ).hexdigest() |
| 2142 | |
| 2143 | def _serialize(self) -> bytes: |
| 2144 | """Serialize the state for redis. |
| 2145 | |
| 2146 | Returns: |
| 2147 | The serialized state. |
| 2148 | |
| 2149 | Raises: |
| 2150 | StateSerializationError: If the state cannot be serialized. |
| 2151 | |
| 2152 | # noqa: DAR401: e |
| 2153 | # noqa: DAR402: StateSerializationError |
| 2154 | """ |
| 2155 | payload = b"" |
| 2156 | error = "" |
| 2157 | self_schema = self._to_schema() |
| 2158 | pickle_function = pickle.dumps |
| 2159 | try: |
| 2160 | payload = pickle.dumps((self_schema, self)) |
| 2161 | except HANDLED_PICKLE_ERRORS as og_pickle_error: |
| 2162 | error = ( |
| 2163 | f"Failed to serialize state {self.get_full_name()} due to unpicklable object. " |
| 2164 | "This state will not be persisted. " |
| 2165 | ) |
| 2166 | try: |
| 2167 | import dill |
| 2168 | |
| 2169 | pickle_function = dill.dumps |
| 2170 | payload = dill.dumps((self_schema, self)) |
| 2171 | except ImportError: |
| 2172 | error += ( |
| 2173 | f"Pickle error: {og_pickle_error}. " |
| 2174 | "Consider `pip install 'dill>=0.3.8'` for more exotic serialization support." |
| 2175 | ) |
| 2176 | except HANDLED_PICKLE_ERRORS as ex: |
| 2177 | error += f"Dill was also unable to pickle the state: {ex}" |
| 2178 | |
| 2179 | if environment.REFLEX_PERF_MODE.get() != PerformanceMode.OFF: |
| 2180 | self._check_state_size(len(payload)) |
| 2181 | |
| 2182 | if not payload: |
| 2183 | e = StateSerializationError(error) |
| 2184 | if sys.version_info >= (3, 11): |
| 2185 | try: |
| 2186 | debug_failed_pickles(self, pickle_function) |
| 2187 | except HANDLED_PICKLE_ERRORS as ex: |
| 2188 | for note in ex.__notes__: |
| 2189 | e.add_note(note) |
| 2190 | raise e |
| 2191 | |
| 2192 | return payload |
| 2193 | |
| 2194 | @classmethod |
| 2195 | def _deserialize( |