@@ -1584,25 +1584,26 @@ def _outbound_schedule_activity(
1584
1584
self ,
1585
1585
input : Union [StartActivityInput , StartLocalActivityInput ],
1586
1586
) -> _ActivityHandle :
1587
+ # A ScheduleActivityTask command always results in an ActivityTaskScheduled event,
1588
+ # so this function returns the handle immediately. This is similar to nexus
1589
+ # operation but differs from child workflow.
1590
+
1587
1591
# Validate
1588
1592
if not input .start_to_close_timeout and not input .schedule_to_close_timeout :
1589
1593
raise ValueError (
1590
1594
"Activity must have start_to_close_timeout or schedule_to_close_timeout"
1591
1595
)
1592
1596
1593
- handle : Optional [ _ActivityHandle ] = None
1597
+ handle : _ActivityHandle
1594
1598
1595
1599
# Function that runs in the handle
1596
1600
async def run_activity () -> Any :
1597
- nonlocal handle
1598
- assert handle
1599
1601
while True :
1600
1602
# Mark it as started each loop because backoff could cause it to
1601
1603
# be marked as unstarted
1602
1604
handle ._started = True
1603
1605
try :
1604
- # We have to shield because we don't want the underlying
1605
- # result future to be cancelled
1606
+ # Shield so that future itself is not cancelled
1606
1607
return await asyncio .shield (handle ._result_fut )
1607
1608
except _ActivityDoBackoffError as err :
1608
1609
# We have to sleep then reschedule. Note this sleep can be
@@ -1662,12 +1663,16 @@ async def _outbound_signal_external_workflow(
1662
1663
async def _outbound_start_child_workflow (
1663
1664
self , input : StartChildWorkflowInput
1664
1665
) -> _ChildWorkflowHandle :
1665
- handle : Optional [_ChildWorkflowHandle ] = None
1666
+ # A StartChildWorkflowExecution command results in a
1667
+ # StartChildWorkflowExecutionInitiated event, but the start may fail (e.g. due to
1668
+ # workflow ID collision). Therefore this function does not return the handle until
1669
+ # a future activation contains an event indicating start success / failure. This
1670
+ # differs from activity and nexus operation.
1671
+
1672
+ handle : _ChildWorkflowHandle
1666
1673
1667
1674
# Common code for handling cancel for start and run
1668
1675
def apply_child_cancel_error () -> None :
1669
- nonlocal handle
1670
- assert handle
1671
1676
# Send a cancel request to the child
1672
1677
cancel_command = self ._add_command ()
1673
1678
handle ._apply_cancel_command (cancel_command )
@@ -1685,12 +1690,9 @@ def apply_child_cancel_error() -> None:
1685
1690
1686
1691
# Function that runs in the handle
1687
1692
async def run_child () -> Any :
1688
- nonlocal handle
1689
1693
while True :
1690
- assert handle
1691
1694
try :
1692
- # We have to shield because we don't want the future itself
1693
- # to be cancelled
1695
+ # Shield so that future itself is not cancelled
1694
1696
return await asyncio .shield (handle ._result_fut )
1695
1697
except asyncio .CancelledError :
1696
1698
apply_child_cancel_error ()
@@ -1705,8 +1707,7 @@ async def run_child() -> Any:
1705
1707
# Wait on start before returning
1706
1708
while True :
1707
1709
try :
1708
- # We have to shield because we don't want the future itself
1709
- # to be cancelled
1710
+ # Shield so that future itself is not cancelled
1710
1711
await asyncio .shield (handle ._start_fut )
1711
1712
return handle
1712
1713
except asyncio .CancelledError :
@@ -2438,17 +2439,17 @@ async def signal_external_workflow(
2438
2439
2439
2440
def start_activity (
2440
2441
self , input : StartActivityInput
2441
- ) -> temporalio .workflow .ActivityHandle :
2442
+ ) -> temporalio .workflow .ActivityHandle [ Any ] :
2442
2443
return self ._instance ._outbound_schedule_activity (input )
2443
2444
2444
2445
async def start_child_workflow (
2445
2446
self , input : StartChildWorkflowInput
2446
- ) -> temporalio .workflow .ChildWorkflowHandle :
2447
+ ) -> temporalio .workflow .ChildWorkflowHandle [ Any , Any ] :
2447
2448
return await self ._instance ._outbound_start_child_workflow (input )
2448
2449
2449
2450
def start_local_activity (
2450
2451
self , input : StartLocalActivityInput
2451
- ) -> temporalio .workflow .ActivityHandle :
2452
+ ) -> temporalio .workflow .ActivityHandle [ Any ] :
2452
2453
return self ._instance ._outbound_schedule_activity (input )
2453
2454
2454
2455
0 commit comments