@@ -124,6 +124,10 @@ def __await__(self):
124
124
self ._ensure_future ()
125
125
return self ._future_local .aio_future .__await__ ()
126
126
127
+ def get_future (self ):
128
+ self ._ensure_future ()
129
+ return self ._future_local .aio_future
130
+
127
131
128
132
warning_msg = """
129
133
No session found, local session \
@@ -1670,6 +1674,27 @@ def __exit__(self, *_):
1670
1674
self .close ()
1671
1675
1672
1676
1677
+ async def _execute_with_progress (
1678
+ execution_info : ExecutionInfo ,
1679
+ progress_bar : ProgressBar ,
1680
+ progress_update_interval : Union [int , float ],
1681
+ cancelled : asyncio .Event ,
1682
+ ):
1683
+ with progress_bar :
1684
+ while not cancelled .is_set ():
1685
+ done , _pending = await asyncio .wait (
1686
+ [execution_info .get_future ()], timeout = progress_update_interval
1687
+ )
1688
+ if not done :
1689
+ if not cancelled .is_set () and execution_info .progress () is not None :
1690
+ progress_bar .update (execution_info .progress () * 100 )
1691
+ else :
1692
+ # done
1693
+ if not cancelled .is_set ():
1694
+ progress_bar .update (100 )
1695
+ break
1696
+
1697
+
1673
1698
async def _execute (
1674
1699
* tileables : Tuple [TileableType ],
1675
1700
session : _IsolatedSession = None ,
@@ -1691,39 +1716,20 @@ def _attach_session(future: asyncio.Future):
1691
1716
if wait :
1692
1717
progress_bar = ProgressBar (show_progress )
1693
1718
if progress_bar .show_progress :
1694
- with progress_bar :
1695
- while not cancelled .is_set ():
1696
- try :
1697
- await asyncio .wait_for (
1698
- asyncio .shield (execution_info ), progress_update_interval
1699
- )
1700
- # done
1701
- if not cancelled .is_set ():
1702
- progress_bar .update (100 )
1703
- break
1704
- except asyncio .TimeoutError :
1705
- # timeout
1706
- if (
1707
- not cancelled .is_set ()
1708
- and execution_info .progress () is not None
1709
- ):
1710
- progress_bar .update (execution_info .progress () * 100 )
1711
- if cancelled .is_set ():
1712
- # cancel execution
1713
- execution_info .cancel ()
1714
- execution_info .remove_done_callback (_attach_session )
1715
- await execution_info
1719
+ await _execute_with_progress (
1720
+ execution_info , progress_bar , progress_update_interval , cancelled
1721
+ )
1716
1722
else :
1717
1723
await asyncio .wait (
1718
1724
[execution_info , cancelled .wait ()], return_when = asyncio .FIRST_COMPLETED
1719
1725
)
1720
- if cancelled .is_set ():
1721
- execution_info .remove_done_callback (_attach_session )
1722
- execution_info .cancel ()
1723
- else :
1724
- # set cancelled to avoid wait task leak
1725
- cancelled .set ()
1726
- await execution_info
1726
+ if cancelled .is_set ():
1727
+ execution_info .remove_done_callback (_attach_session )
1728
+ execution_info .cancel ()
1729
+ else :
1730
+ # set cancelled to avoid wait task leak
1731
+ cancelled .set ()
1732
+ await execution_info
1727
1733
else :
1728
1734
return execution_info
1729
1735
0 commit comments