@@ -120,17 +120,23 @@ def _transform_helper(
120
120
else :
121
121
raise ValueError ("transform() can only be called on a CocoIndex function" )
122
122
123
- return _create_data_slice (
124
- flow_builder_state ,
125
- lambda target_scope , name : flow_builder_state .engine_flow_builder .transform (
123
+ def _create_data_slice_inner (
124
+ target_scope : _engine .DataScopeRef | None , name : str | None
125
+ ) -> _engine .DataSlice :
126
+ result = flow_builder_state .engine_flow_builder .transform (
126
127
kind ,
127
128
dump_engine_object (spec ),
128
129
transform_args ,
129
130
target_scope ,
130
131
flow_builder_state .field_name_builder .build_name (
131
132
name , prefix = _to_snake_case (_spec_kind (fn_spec )) + "_"
132
133
),
133
- ),
134
+ )
135
+ return result
136
+
137
+ return _create_data_slice (
138
+ flow_builder_state ,
139
+ _create_data_slice_inner ,
134
140
name ,
135
141
)
136
142
@@ -166,6 +172,7 @@ def __init__(
166
172
def engine_data_slice (self ) -> _engine .DataSlice :
167
173
"""
168
174
Get the internal DataSlice.
175
+ This can be blocking.
169
176
"""
170
177
if self ._lazy_lock is None :
171
178
if self ._data_slice is None :
@@ -179,6 +186,13 @@ def engine_data_slice(self) -> _engine.DataSlice:
179
186
self ._data_slice = self ._data_slice_creator (None )
180
187
return self ._data_slice
181
188
189
+ async def engine_data_slice_async (self ) -> _engine .DataSlice :
190
+ """
191
+ Get the internal DataSlice.
192
+ This can be blocking.
193
+ """
194
+ return await asyncio .to_thread (lambda : self .engine_data_slice )
195
+
182
196
def attach_to_scope (self , scope : _engine .DataScopeRef , field_name : str ) -> None :
183
197
"""
184
198
Attach the current data slice (if not yet attached) to the given scope.
@@ -795,9 +809,8 @@ async def setup_async(self, report_to_stdout: bool = False) -> None:
795
809
"""
796
810
Setup persistent backends of the flow. The async version.
797
811
"""
798
- await make_setup_bundle ([self ]).describe_and_apply_async (
799
- report_to_stdout = report_to_stdout
800
- )
812
+ bundle = await make_setup_bundle_async ([self ])
813
+ await bundle .describe_and_apply_async (report_to_stdout = report_to_stdout )
801
814
802
815
def drop (self , report_to_stdout : bool = False ) -> None :
803
816
"""
@@ -814,9 +827,8 @@ async def drop_async(self, report_to_stdout: bool = False) -> None:
814
827
"""
815
828
Drop persistent backends of the flow. The async version.
816
829
"""
817
- await make_drop_bundle ([self ]).describe_and_apply_async (
818
- report_to_stdout = report_to_stdout
819
- )
830
+ bundle = await make_drop_bundle_async ([self ])
831
+ await bundle .describe_and_apply_async (report_to_stdout = report_to_stdout )
820
832
821
833
def close (self ) -> None :
822
834
"""
@@ -1071,19 +1083,16 @@ async def _build_flow_info_async(self) -> TransformFlowInfo:
1071
1083
_DataSliceState (flow_builder_state , engine_ds )
1072
1084
)
1073
1085
1074
- output = self ._flow_fn (** kwargs )
1075
- flow_builder_state . engine_flow_builder . set_direct_output (
1076
- _data_slice_state ( output ). engine_data_slice
1077
- )
1086
+ output = await asyncio . to_thread ( lambda : self ._flow_fn (** kwargs ) )
1087
+ output_data_slice = await _data_slice_state ( output ). engine_data_slice_async ()
1088
+
1089
+ flow_builder_state . engine_flow_builder . set_direct_output ( output_data_slice )
1078
1090
engine_flow = (
1079
1091
await flow_builder_state .engine_flow_builder .build_transient_flow_async (
1080
1092
execution_context .event_loop
1081
1093
)
1082
1094
)
1083
-
1084
- engine_return_type = (
1085
- _data_slice_state (output ).engine_data_slice .data_type ().schema ()
1086
- )
1095
+ engine_return_type = output_data_slice .data_type ().schema ()
1087
1096
python_return_type : type [T ] | None = _get_data_slice_annotation_type (
1088
1097
inspect .signature (self ._flow_fn ).return_annotation
1089
1098
)
@@ -1142,28 +1151,42 @@ def _transform_flow_wrapper(fn: Callable[..., DataSlice[T]]) -> TransformFlow[T]
1142
1151
return _transform_flow_wrapper
1143
1152
1144
1153
1145
- def make_setup_bundle (flow_iter : Iterable [Flow ]) -> SetupChangeBundle :
1154
+ async def make_setup_bundle_async (flow_iter : Iterable [Flow ]) -> SetupChangeBundle :
1146
1155
"""
1147
1156
Make a bundle to setup flows with the given names.
1148
1157
"""
1149
1158
full_names = []
1150
1159
for fl in flow_iter :
1151
- fl .internal_flow ()
1160
+ await fl .internal_flow_async ()
1152
1161
full_names .append (fl .full_name )
1153
1162
return SetupChangeBundle (_engine .make_setup_bundle (full_names ))
1154
1163
1155
1164
1156
- def make_drop_bundle (flow_iter : Iterable [Flow ]) -> SetupChangeBundle :
1165
+ def make_setup_bundle (flow_iter : Iterable [Flow ]) -> SetupChangeBundle :
1166
+ """
1167
+ Make a bundle to setup flows with the given names.
1168
+ """
1169
+ return execution_context .run (make_setup_bundle_async (flow_iter ))
1170
+
1171
+
1172
+ async def make_drop_bundle_async (flow_iter : Iterable [Flow ]) -> SetupChangeBundle :
1157
1173
"""
1158
1174
Make a bundle to drop flows with the given names.
1159
1175
"""
1160
1176
full_names = []
1161
1177
for fl in flow_iter :
1162
- fl .internal_flow ()
1178
+ await fl .internal_flow_async ()
1163
1179
full_names .append (fl .full_name )
1164
1180
return SetupChangeBundle (_engine .make_drop_bundle (full_names ))
1165
1181
1166
1182
1183
+ def make_drop_bundle (flow_iter : Iterable [Flow ]) -> SetupChangeBundle :
1184
+ """
1185
+ Make a bundle to drop flows with the given names.
1186
+ """
1187
+ return execution_context .run (make_drop_bundle_async (flow_iter ))
1188
+
1189
+
1167
1190
def setup_all_flows (report_to_stdout : bool = False ) -> None :
1168
1191
"""
1169
1192
Setup all flows registered in the current process.
0 commit comments