@@ -1537,25 +1537,26 @@ def _outbound_schedule_activity(
1537
1537
self ,
1538
1538
input : Union [StartActivityInput , StartLocalActivityInput ],
1539
1539
) -> _ActivityHandle :
1540
+ # A ScheduleActivityTask command always results in an ActivityTaskScheduled event,
1541
+ # so this function returns the handle immediately. This is similar to nexus
1542
+ # operation but differs from child workflow.
1543
+
1540
1544
# Validate
1541
1545
if not input .start_to_close_timeout and not input .schedule_to_close_timeout :
1542
1546
raise ValueError (
1543
1547
"Activity must have start_to_close_timeout or schedule_to_close_timeout"
1544
1548
)
1545
1549
1546
- handle : Optional [ _ActivityHandle ] = None
1550
+ handle : _ActivityHandle
1547
1551
1548
1552
# Function that runs in the handle
1549
1553
async def run_activity () -> Any :
1550
- nonlocal handle
1551
- assert handle
1552
1554
while True :
1553
1555
# Mark it as started each loop because backoff could cause it to
1554
1556
# be marked as unstarted
1555
1557
handle ._started = True
1556
1558
try :
1557
- # We have to shield because we don't want the underlying
1558
- # result future to be cancelled
1559
+ # Shield so that future itself is not cancelled
1559
1560
return await asyncio .shield (handle ._result_fut )
1560
1561
except _ActivityDoBackoffError as err :
1561
1562
# We have to sleep then reschedule. Note this sleep can be
@@ -1615,12 +1616,16 @@ async def _outbound_signal_external_workflow(
1615
1616
async def _outbound_start_child_workflow (
1616
1617
self , input : StartChildWorkflowInput
1617
1618
) -> _ChildWorkflowHandle :
1618
- handle : Optional [_ChildWorkflowHandle ] = None
1619
+ # A StartChildWorkflowExecution command results in a
1620
+ # StartChildWorkflowExecutionInitiated event, but the start may fail (e.g. due to
1621
+ # workflow ID collision). Therefore this function does not return the handle until
1622
+ # a future activation contains an event indicating start success / failure. This
1623
+ # differs from activity and nexus operation.
1624
+
1625
+ handle : _ChildWorkflowHandle
1619
1626
1620
1627
# Common code for handling cancel for start and run
1621
1628
def apply_child_cancel_error () -> None :
1622
- nonlocal handle
1623
- assert handle
1624
1629
# Send a cancel request to the child
1625
1630
cancel_command = self ._add_command ()
1626
1631
handle ._apply_cancel_command (cancel_command )
@@ -1638,12 +1643,9 @@ def apply_child_cancel_error() -> None:
1638
1643
1639
1644
# Function that runs in the handle
1640
1645
async def run_child () -> Any :
1641
- nonlocal handle
1642
1646
while True :
1643
- assert handle
1644
1647
try :
1645
- # We have to shield because we don't want the future itself
1646
- # to be cancelled
1648
+ # Shield so that future itself is not cancelled
1647
1649
return await asyncio .shield (handle ._result_fut )
1648
1650
except asyncio .CancelledError :
1649
1651
apply_child_cancel_error ()
@@ -1658,8 +1660,7 @@ async def run_child() -> Any:
1658
1660
# Wait on start before returning
1659
1661
while True :
1660
1662
try :
1661
- # We have to shield because we don't want the future itself
1662
- # to be cancelled
1663
+ # Shield so that future itself is not cancelled
1663
1664
await asyncio .shield (handle ._start_fut )
1664
1665
return handle
1665
1666
except asyncio .CancelledError :
@@ -2391,17 +2392,17 @@ async def signal_external_workflow(
2391
2392
2392
2393
def start_activity (
2393
2394
self , input : StartActivityInput
2394
- ) -> temporalio .workflow .ActivityHandle :
2395
+ ) -> temporalio .workflow .ActivityHandle [ Any ] :
2395
2396
return self ._instance ._outbound_schedule_activity (input )
2396
2397
2397
2398
async def start_child_workflow (
2398
2399
self , input : StartChildWorkflowInput
2399
- ) -> temporalio .workflow .ChildWorkflowHandle :
2400
+ ) -> temporalio .workflow .ChildWorkflowHandle [ Any , Any ] :
2400
2401
return await self ._instance ._outbound_start_child_workflow (input )
2401
2402
2402
2403
def start_local_activity (
2403
2404
self , input : StartLocalActivityInput
2404
- ) -> temporalio .workflow .ActivityHandle :
2405
+ ) -> temporalio .workflow .ActivityHandle [ Any ] :
2405
2406
return self ._instance ._outbound_schedule_activity (input )
2406
2407
2407
2408
0 commit comments