|
9 | 9 | import tempfile |
10 | 10 | import uuid |
11 | 11 | from collections.abc import Awaitable |
| 12 | +from concurrent import futures |
12 | 13 | from functools import partial |
13 | 14 | from pathlib import Path |
14 | 15 | from typing import Any, Coroutine, Optional, Tuple, Union, cast, Generator, BinaryIO |
15 | 16 |
|
16 | 17 | import aiofiles |
17 | 18 | import httpx |
18 | | -import nest_asyncio # type: ignore |
19 | 19 | from httpx import AsyncClient |
20 | 20 | from pypdf import PdfReader, PdfWriter |
21 | 21 |
|
@@ -268,10 +268,6 @@ def before_request( |
268 | 268 | logger.warning("Splitting is currently incompatible with uvloop. Continuing without splitting.") |
269 | 269 | return request |
270 | 270 |
|
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 | | - |
275 | 271 | # This is our key into coroutines_to_execute |
276 | 272 | # We need to pass it on to after_success so |
277 | 273 | # we know which results are ours |
@@ -605,10 +601,13 @@ def _await_elements(self, operation_id: str) -> Optional[list]: |
605 | 601 | if tasks is None: |
606 | 602 | return None |
607 | 603 |
|
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() |
612 | 611 |
|
613 | 612 | if task_responses is None: |
614 | 613 | return None |
|
0 commit comments