File tree Expand file tree Collapse file tree 1 file changed +17
-1
lines changed
src/unstructured_client/_hooks/custom Expand file tree Collapse file tree 1 file changed +17
-1
lines changed Original file line number Diff line number Diff line change 66import logging
77import math
88import os
9+ import sys
910import tempfile
1011import uuid
1112from collections .abc import Awaitable
5657HI_RES_STRATEGY = 'hi_res'
5758MAX_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
6076async 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 :
You can’t perform that action at this time.
0 commit comments