Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
feat: enhance async task management and cleanup in gradio_app
myhlolifffccefec791
diff --git a/mineru/cli/gradio_app.py b/mineru/cli/gradio_app.pyindex aa0c56bc..6444d6f8 100644--- a/mineru/cli/gradio_app.py+++ b/mineru/cli/gradio_app.py@@ -93,6 +93,28 @@ class GradioRequestConcurrencyLimiter:if wait_token in state.waiters:state.waiters.remove(wait_token)+ async def _cleanup_acquire_interruption(+ self,+ state: _LimiterState,+ acquire_task: asyncio.Task[bool],+ wait_token: object,+ should_wait: bool,+ ) -> None:+ if not acquire_task.done():+ acquire_task.cancel()+ await asyncio.gather(acquire_task, return_exceptions=True)+ elif not acquire_task.cancelled():+ try:+ acquired = acquire_task.result()+ except Exception:+ acquired = False+ if acquired:+ state.semaphore.release()++ if should_wait:+ with self._lock:+ self._remove_waiter(state, wait_token)+@asynccontextmanagerasync def acquire(self,@@ -113,7 +135,7 @@ class GradioRequestConcurrencyLimiter:should_wait = Truesnapshot = self._build_wait_snapshot(state, limit, wait_token)- acquire_task = None+ acquire_task: asyncio.Task[bool] = asyncio.create_task(state.semaphore.acquire())last_wait_ahead = Noneif should_wait and on_wait is not None and snapshot is not None:on_wait(snapshot)@@ -121,7 +143,6 @@ class GradioRequestConcurrencyLimiter:try:if should_wait:- acquire_task = asyncio.create_task(state.semaphore.acquire())while True:done, _ = await asyncio.wait({acquire_task},@@ -143,14 +164,14 @@ class GradioRequestConcurrencyLimiter:on_wait(snapshot)last_wait_ahead = snapshot.aheadelse:- await state.semaphore.acquire()- except Exception:- if acquire_task is not None:- acquire_task.cancel()- await asyncio.gather(acquire_task, return_exceptions=True)- if should_wait:- with self._lock:- self._remove_waiter(state, wait_token)+ await acquire_task+ except BaseException:+ await self._cleanup_acquire_interruption(+ state=state,+ acquire_task=acquire_task,+ wait_token=wait_token,+ should_wait=should_wait,+ )raisewith self._lock:@@ -686,6 +707,9 @@ async def stream_to_markdown(api_url=None,):status_state = StatusPanelState()+ job_task: asyncio.Task | None = None+ queue_get_task: asyncio.Task | None = None+ timer_task: asyncio.Task | None = Noneyield status_state.render(), None, "", "", gr.skip()if file_path is None:@@ -697,22 +721,22 @@ async def stream_to_markdown(def enqueue_status(message: str) -> None:loop.call_soon_threadsafe(status_queue.put_nowait, message)- job_task = asyncio.create_task(- _run_to_markdown_job(- file_path=file_path,- end_pages=end_pages,- is_ocr=is_ocr,- formula_enable=formula_enable,- table_enable=table_enable,- language=language,- backend=backend,- url=url,- api_url=api_url,- status_callback=enqueue_status,+ try:+ job_task = asyncio.create_task(+ _run_to_markdown_job(+ file_path=file_path,+ end_pages=end_pages,+ is_ocr=is_ocr,+ formula_enable=formula_enable,+ table_enable=table_enable,+ language=language,+ backend=backend,+ url=url,+ api_url=api_url,+ status_callback=enqueue_status,+ ))- )- try:while True:if job_task.done() and status_queue.empty():status_state.finalize_processing()@@ -750,6 +774,8 @@ async def stream_to_markdown(continuepending_task.cancel()await asyncio.gather(pending_task, return_exceptions=True)+ queue_get_task = None+ timer_task = Nonewhile not status_queue.empty():status_state.append(status_queue.get_nowait())@@ -757,6 +783,12 @@ async def stream_to_markdown(status_state.append(format_failed_status(exc))yield status_state.render(), None, "", "", gr.skip()raise+ finally:+ for task in (queue_get_task, timer_task, job_task):+ if task is None or task.done():+ continue+ task.cancel()+ await asyncio.gather(task, return_exceptions=True)try:md_content, txt_content, archive_zip_path, preview_pdf_path = await job_task