Skip to content

Commit d10fec1

Browse files
committed
feat NEXUS-703: replaced nest loop with a standard loop run in a separate thread
1 parent 54d44eb commit d10fec1

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/unstructured_client/_hooks/custom/split_pdf_hook.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
import tempfile
1010
import uuid
1111
from collections.abc import Awaitable
12+
from concurrent import futures
1213
from functools import partial
1314
from pathlib import Path
1415
from typing import Any, Coroutine, Optional, Tuple, Union, cast, Generator, BinaryIO
1516

1617
import aiofiles
1718
import httpx
18-
import nest_asyncio # type: ignore
1919
from httpx import AsyncClient
2020
from pypdf import PdfReader, PdfWriter
2121

@@ -268,10 +268,6 @@ def before_request(
268268
logger.warning("Splitting is currently incompatible with uvloop. Continuing without splitting.")
269269
return request
270270

271-
# This allows us to use an event loop in an env with an existing loop
272-
# Temporary fix until we can improve the async splitting behavior
273-
nest_asyncio.apply()
274-
275271
# This is our key into coroutines_to_execute
276272
# We need to pass it on to after_success so
277273
# we know which results are ours
@@ -605,10 +601,13 @@ def _await_elements(self, operation_id: str) -> Optional[list]:
605601
if tasks is None:
606602
return None
607603

608-
ioloop = asyncio.get_event_loop()
609-
task_responses: list[tuple[int, httpx.Response]] = ioloop.run_until_complete(
610-
run_tasks(tasks, allow_failed=self.allow_failed)
611-
)
604+
coroutines = run_tasks(tasks, allow_failed=self.allow_failed)
605+
606+
# sending the coroutines to a separate thread to avoid blocking the current event loop
607+
# this operation should be removed when the SDK is updated to support async hooks
608+
with futures.ThreadPoolExecutor(max_workers=1) as executor:
609+
task_responses_future = executor.submit(asyncio.run, coroutines)
610+
task_responses = task_responses_future.result()
612611

613612
if task_responses is None:
614613
return None

0 commit comments

Comments
 (0)