@@ -119,7 +119,6 @@ def __init__(
119
119
super ().__init__ ()
120
120
121
121
def run (self ) -> None :
122
- debug ("run" )
123
122
self ._sync_events_lock = threading .Lock ()
124
123
# If we're running at a shell, SIGINT will be sent to every process in
125
124
# the process group. We ignore it in the child process and require that
@@ -146,33 +145,24 @@ def run(self) -> None:
146
145
self ._stream_redirector .start ()
147
146
# </could be moved into StreamRedirector>
148
147
149
- debug ("setup" )
150
148
self ._setup ()
151
- debug ("loop" )
152
149
self ._loop () # shuts down stream redirector the correct way
153
- debug ("loop done" )
154
150
self ._events .close ()
155
151
156
152
async def _async_init (self ) -> None :
157
- debug ("async_init start" )
158
153
if self ._events_async :
159
- debug ("async_init finished" )
160
154
return
161
155
# if AsyncConnection is created before switch_to_async, a race condition can cause drain to fail
162
156
# and write, seemingly, to block
163
157
# maybe because we're trying to call StreamWriter.write when no event loop is running?
164
158
await self ._stream_redirector .switch_to_async ()
165
159
self ._events_async = AsyncConnection (self ._events )
166
160
await self ._events_async .async_init ()
167
- debug ("async_init done" )
168
161
169
162
def _setup (self ) -> None :
170
- debug ("_setup start" )
171
163
with self ._handle_setup_error ():
172
164
# we need to load the predictor to know if setup is async
173
- debug ("'about to load" )
174
165
self ._predictor = load_predictor_from_ref (self ._predictor_ref )
175
- debug ("loaded ref" )
176
166
self ._predictor .log = self ._log
177
167
# if users want to access the same event loop from setup and predict,
178
168
# both have to be async. if setup isn't async, it doesn't matter if we
@@ -181,36 +171,24 @@ def _setup(self) -> None:
181
171
# otherwise, if setup is sync and the user does new_event_loop to use a ClientSession,
182
172
# then tries to use the same session from async predict, they would get an error.
183
173
# that's significant if connections are open and would need to be discarded
184
- debug ("async predictor" )
185
174
if is_async_predictor (self ._predictor ):
186
- debug ("getting loop" )
187
175
self .loop = get_loop ()
188
- debug ("got loop" )
189
- debug ("getattr" )
190
176
# Could be a function or a class
191
177
if hasattr (self ._predictor , "setup" ):
192
- debug ("inspect" )
193
178
if inspect .iscoroutinefunction (self ._predictor .setup ):
194
179
# we should probably handle Shutdown during this process?
195
180
# possibly we prefer to not stop-start the event loop
196
181
# between these calls
197
182
self .loop .run_until_complete (self ._async_init ())
198
183
self .loop .run_until_complete (run_setup_async (self ._predictor ))
199
- debug ("run_setup_async done" )
200
184
else :
201
- debug ("sync setup" )
202
185
run_setup (self ._predictor )
203
- debug ("_setup done inside ctx mgr" )
204
- debug ("_setup done" )
205
186
206
187
@contextlib .contextmanager
207
188
def _handle_setup_error (self ) -> Iterator [None ]:
208
189
done = Done ()
209
- debug ("done" )
210
190
try :
211
- debug ("yield" )
212
191
yield
213
- debug ("yield done" )
214
192
except Exception as e :
215
193
traceback .print_exc ()
216
194
done .error = True
@@ -226,15 +204,11 @@ def _handle_setup_error(self) -> Iterator[None]:
226
204
# we can arrive here if there was an error setting up stream_redirector
227
205
# for example, because drain failed
228
206
# in this case this drain could block or fail
229
- debug ("setup done, calling drain" )
230
207
try :
231
208
self ._stream_redirector .drain ()
232
209
except Exception as e :
233
- debug ("exc" , str (e ))
234
210
raise
235
- debug ("sending setup done" )
236
211
self .send (("SETUP" , done ))
237
- debug ("sent setup done" )
238
212
239
213
def _loop_sync (self ) -> None :
240
214
while True :
@@ -253,7 +227,6 @@ def _loop_sync(self) -> None:
253
227
self ._stream_redirector .shutdown ()
254
228
255
229
async def _loop_async (self ) -> None :
256
- debug ("loop async" )
257
230
await self ._async_init ()
258
231
assert self ._events_async
259
232
tasks : dict [str , asyncio .Task [None ]] = {}
@@ -277,14 +250,11 @@ async def _loop_async(self) -> None:
277
250
print (f"Got unexpected cancellation: { ev } " , file = sys .stderr )
278
251
else :
279
252
print (f"Got unexpected event: { ev } " , file = sys .stderr )
280
- debug ("shutdown_async" )
281
253
await self ._stream_redirector .shutdown_async ()
282
254
self ._events_async .close ()
283
255
284
256
def _loop (self ) -> None :
285
- debug ("in loop" )
286
257
if is_async (get_predict (self ._predictor )):
287
- debug ("async loop" )
288
258
self .loop .run_until_complete (self ._loop_async ())
289
259
else :
290
260
self ._loop_sync ()
@@ -320,14 +290,10 @@ def _emit_metric(self, name: str, value: "int | float") -> None:
320
290
321
291
def send (self , obj : Any ) -> None :
322
292
if self ._events_async :
323
- debug ("sending on async" )
324
293
self ._events_async .send (obj )
325
- debug ("sent on async" )
326
294
else :
327
- debug ("send lock" )
328
295
with self ._sync_events_lock :
329
296
self ._events .send (obj )
330
- debug ("finished sync send" )
331
297
332
298
def _mk_send (self , id : str ) -> Callable [[PublicEventType ], None ]:
333
299
def send (event : PublicEventType ) -> None :
0 commit comments