Skip to content

Commit b9fa031

Browse files
committed
Construct context-aware data converter lazily on WorkflowExecution
1 parent e129ceb commit b9fa031

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

temporalio/client.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,9 +2883,6 @@ class WorkflowExecution:
28832883
close_time: Optional[datetime]
28842884
"""When the workflow was closed if closed."""
28852885

2886-
data_converter: temporalio.converter.DataConverter
2887-
"""Data converter from when this description was created."""
2888-
28892886
execution_time: Optional[datetime]
28902887
"""When this workflow run started or should start."""
28912888

@@ -2895,6 +2892,9 @@ class WorkflowExecution:
28952892
id: str
28962893
"""ID for the workflow."""
28972894

2895+
namespace: str
2896+
"""Namespace for the workflow."""
2897+
28982898
parent_id: Optional[str]
28992899
"""ID for the parent workflow if this was started as a child."""
29002900

@@ -2935,27 +2935,39 @@ class WorkflowExecution:
29352935
workflow_type: str
29362936
"""Type name for the workflow."""
29372937

2938+
_context_free_data_converter: temporalio.converter.DataConverter
2939+
2940+
@property
2941+
def data_converter(self) -> temporalio.converter.DataConverter:
2942+
return self._context_free_data_converter.with_context(
2943+
WorkflowSerializationContext(
2944+
namespace=self.namespace,
2945+
workflow_id=self.id,
2946+
)
2947+
)
2948+
29382949
@classmethod
29392950
def _from_raw_info(
29402951
cls,
29412952
info: temporalio.api.workflow.v1.WorkflowExecutionInfo,
2953+
namespace: str,
29422954
converter: temporalio.converter.DataConverter,
29432955
**additional_fields: Any,
2944-
) -> WorkflowExecution:
2956+
) -> Self:
29452957
return cls(
29462958
close_time=(
29472959
info.close_time.ToDatetime().replace(tzinfo=timezone.utc)
29482960
if info.HasField("close_time")
29492961
else None
29502962
),
2951-
data_converter=converter,
29522963
execution_time=(
29532964
info.execution_time.ToDatetime().replace(tzinfo=timezone.utc)
29542965
if info.HasField("execution_time")
29552966
else None
29562967
),
29572968
history_length=info.history_length,
29582969
id=info.execution.workflow_id,
2970+
namespace=namespace,
29592971
parent_id=(
29602972
info.parent_execution.workflow_id
29612973
if info.HasField("parent_execution")
@@ -2986,6 +2998,7 @@ def _from_raw_info(
29862998
info.search_attributes
29872999
),
29883000
workflow_type=info.type.name,
3001+
_context_free_data_converter=converter,
29893002
**additional_fields,
29903003
)
29913004

@@ -3091,11 +3104,13 @@ async def _decode_metadata(self) -> None:
30913104
@staticmethod
30923105
async def _from_raw_description(
30933106
description: temporalio.api.workflowservice.v1.DescribeWorkflowExecutionResponse,
3107+
namespace: str,
30943108
converter: temporalio.converter.DataConverter,
30953109
) -> WorkflowExecutionDescription:
3096-
return WorkflowExecutionDescription._from_raw_info( # type: ignore
3110+
return WorkflowExecutionDescription._from_raw_info(
30973111
description.workflow_execution_info,
3098-
converter,
3112+
namespace=namespace,
3113+
converter=converter,
30993114
raw_description=description,
31003115
)
31013116

@@ -3246,23 +3261,9 @@ async def fetch_next_page(self, *, page_size: Optional[int] = None) -> None:
32463261
timeout=self._input.rpc_timeout,
32473262
)
32483263

3249-
data_converter_cache = {}
3250-
3251-
def get_data_converter(workflow_id: str) -> temporalio.converter.DataConverter:
3252-
if workflow_id not in data_converter_cache:
3253-
data_converter_cache[workflow_id] = (
3254-
self._client.data_converter.with_context(
3255-
WorkflowSerializationContext(
3256-
namespace=self._client.namespace,
3257-
workflow_id=workflow_id,
3258-
)
3259-
)
3260-
)
3261-
return data_converter_cache[workflow_id]
3262-
32633264
self._current_page = [
32643265
WorkflowExecution._from_raw_info(
3265-
v, get_data_converter(v.execution.workflow_id)
3266+
v, self._client.namespace, self._client.data_converter
32663267
)
32673268
for v in resp.executions
32683269
]
@@ -6111,7 +6112,8 @@ async def describe_workflow(
61116112
metadata=input.rpc_metadata,
61126113
timeout=input.rpc_timeout,
61136114
),
6114-
self._client.data_converter.with_context(
6115+
namespace=self._client.namespace,
6116+
converter=self._client.data_converter.with_context(
61156117
WorkflowSerializationContext(
61166118
namespace=self._client.namespace,
61176119
workflow_id=input.id,

0 commit comments

Comments
 (0)