@@ -108,13 +108,14 @@ class SharedStateRenderer(SingleStateRenderer):
108
108
"""
109
109
110
110
_render_task : asyncio .Task
111
- _join_event : asyncio .Event
112
111
113
112
def __init__ (self , layout : AbstractLayout ) -> None :
114
113
super ().__init__ (layout )
115
114
self ._models : Dict [str , Dict [str , Any ]] = {}
116
115
self ._updates : Dict [str , asyncio .Queue [LayoutUpdate ]] = {}
117
116
self ._task_group = create_task_group ()
117
+ self ._join_event = asyncio .Event ()
118
+ self ._active = False
118
119
self ._joining = False
119
120
120
121
async def start (self ):
@@ -124,10 +125,9 @@ async def join(self):
124
125
await self .__aexit__ (None , None , None )
125
126
126
127
async def __aenter__ (self ):
127
- if hasattr ( self , "_join_event" ) :
128
+ if self . _active :
128
129
raise RuntimeError ("Renderer already active" )
129
- self ._join_event = asyncio .Event ()
130
-
130
+ self ._active = True
131
131
await self ._task_group .__aenter__ ()
132
132
self ._render_task = asyncio .ensure_future (self ._render_loop (), loop = self .loop )
133
133
return self
@@ -138,23 +138,28 @@ async def __aexit__(
138
138
exc_val : Optional [BaseException ],
139
139
exc_tb : Optional [TracebackType ],
140
140
) -> None :
141
- if not self ._joining :
142
- self ._joining = True
143
- try :
144
- await self ._task_group .__aexit__ (exc_type , exc_val , exc_tb )
145
- finally :
146
- self ._render_task .cancel ()
147
- self ._join_event .set ()
148
- else :
149
- await self ._join_event .wait ()
141
+ try :
142
+ if not self ._joining :
143
+ self ._joining = True
144
+ try :
145
+ await self ._task_group .__aexit__ (exc_type , exc_val , exc_tb )
146
+ finally :
147
+ self ._render_task .cancel ()
148
+ self ._join_event .set ()
149
+ self ._join_event .clear ()
150
+ else :
151
+ await self ._join_event .wait ()
152
+ finally :
153
+ self ._active = False
154
+ self ._joining = False
150
155
151
156
async def run (
152
157
self , send : SendCoroutine , recv : RecvCoroutine , context : str , join : bool = False
153
158
) -> None :
154
159
self ._updates [context ] = asyncio .Queue ()
155
160
await self ._task_group .spawn (super ().run , send , recv , context )
156
161
if join :
157
- self .join ()
162
+ await self .join ()
158
163
159
164
async def _render_loop (self ) -> None :
160
165
while True :
0 commit comments