Skip to content

Commit 0112204

Browse files
committed
fix NEXUS-703: corrected loop management for python 3.9
1 parent 480b573 commit 0112204

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/unstructured_client/_hooks/custom/split_pdf_hook.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import logging
77
import math
88
import os
9+
import sys
910
import tempfile
1011
import uuid
1112
from collections.abc import Awaitable
@@ -56,6 +57,21 @@
5657
HI_RES_STRATEGY = 'hi_res'
5758
MAX_PAGE_LENGTH = 4000
5859

60+
def _run_coroutines_in_separate_thread(
61+
coroutines_task: Coroutine[Any, Any, list[tuple[Any, httpx.Response]]]
62+
) -> list[httpx.Response]:
63+
if sys.version_info < (3, 10):
64+
loop = asyncio.get_event_loop()
65+
else:
66+
try:
67+
loop = asyncio.get_running_loop()
68+
except RuntimeError:
69+
loop = asyncio.new_event_loop()
70+
71+
asyncio.set_event_loop(loop)
72+
73+
return loop.run_until_complete(coroutines_task)
74+
5975

6076
async def _order_keeper(index: int, coro: Awaitable) -> Tuple[int, httpx.Response]:
6177
response = await coro
@@ -606,7 +622,7 @@ def _await_elements(self, operation_id: str) -> Optional[list]:
606622
# sending the coroutines to a separate thread to avoid blocking the current event loop
607623
# this operation should be removed when the SDK is updated to support async hooks
608624
with futures.ThreadPoolExecutor(max_workers=1) as executor:
609-
task_responses_future = executor.submit(asyncio.run, coroutines)
625+
task_responses_future = executor.submit(_run_coroutines_in_separate_thread, coroutines)
610626
task_responses = task_responses_future.result()
611627

612628
if task_responses is None:

0 commit comments

Comments
 (0)