@@ -241,11 +241,7 @@ async def _worker_main(self) -> None:
241241 )
242242
243243 case _WaitForPartitionComplete ():
244- task_funcs .append (
245- lambda task = task : anyio .to_thread .run_sync (
246- self ._wait_for_completion , task
247- )
248- )
244+ task_funcs .append (lambda task = task : self ._wait_for_completion (task ))
249245
250246 case _StopWorker ():
251247 stop = True
@@ -295,19 +291,18 @@ async def _run_queue_bridge(
295291 self ,
296292 task_send : MemoryObjectSendStream [_TaskSequence ],
297293 ) -> None :
298- async with task_send :
299- await anyio .to_thread .run_sync (self ._queue_reader , task_send )
294+ def blocking_queue_reader (
295+ task_send : MemoryObjectSendStream [_TaskSequence ],
296+ ) -> None :
297+ while True :
298+ task = self .task_queue .get () # blocks here — fine, it's a thread
299+ anyio .from_thread .run (task_send .send , task )
300300
301- def _queue_reader (
302- self ,
303- task_send : MemoryObjectSendStream [_TaskSequence ],
304- ) -> None :
305- while True :
306- task = self .task_queue .get () # blocks here — fine, it's a thread
307- anyio .from_thread .run (task_send .send , task )
301+ if isinstance (task , _StopWorker ):
302+ break
308303
309- if isinstance ( task , _StopWorker ) :
310- break
304+ async with task_send :
305+ await anyio . to_thread . run_sync ( blocking_queue_reader , task_send )
311306
312307 async def _do_last_updated (
313308 self ,
@@ -419,24 +414,28 @@ async def _do_load_progress(
419414 """ )
420415 load_progress_bg_timer .stop ()
421416
422- def _wait_for_completion (self , task : _WaitForPartitionComplete ) -> None :
423- while any (
424- task
425- for task in self ._running_tasks
426- if isinstance (task , _LoadPartitionTask )
427- and task .partition .name == task .partition .name
428- and task .model == task .model
429- ):
430- anyio .from_thread .check_cancelled ()
431- time .sleep (0.05 ) # poll interval
417+ async def _wait_for_completion (self , task : _WaitForPartitionComplete ) -> None :
418+ def blocking_check_tasks () -> None :
419+ while any (
420+ task
421+ for task in self ._running_tasks
422+ if isinstance (task , _LoadPartitionTask )
423+ and task .partition .name == task .partition .name
424+ and task .model == task .model
425+ ):
426+ anyio .from_thread .check_cancelled ()
427+ time .sleep (0.05 ) # poll interval
428+
429+ logger .debug (
430+ "{}-{} has no tasks remaining, done event: {}" ,
431+ task .model .table (),
432+ task .partition .name ,
433+ task .done_event .__hash__ (),
434+ )
432435
433- logger .debug (
434- "{}-{} has no tasks remaining, done event: {}" ,
435- task .model .table (),
436- task .partition .name ,
437- task .done_event .__hash__ (),
438- )
439- task .done_event .set ()
436+ task .done_event .set ()
437+
438+ await anyio .to_thread .run_sync (blocking_check_tasks )
440439
441440
442441class LoadingBatchWorkerClient :
0 commit comments