-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NA] Make evaluate functions work stably when event loops are used in…
…side (#1171) * Make evaluate functions work stably when event loops are used inside worker threads with httpx.AsyncClients. Fix ascore call in LiteLLMChatCompletion to call acompletion under the hood * Added comment
- Loading branch information
1 parent
6fd37cb
commit f032513
Showing
3 changed files
with
78 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import httpcore | ||
import functools | ||
import contextlib | ||
|
||
from typing import Iterator, Callable | ||
|
||
|
||
@contextlib.contextmanager | ||
def async_http_connections_expire_immediately() -> Iterator[None]: | ||
""" | ||
This patching addresses the issue of httpx.AsyncClient not working | ||
correctly when it's used by multiple event loops. | ||
The connection from connection pool created with one event loop can be tried to be used | ||
by the request processed via another event loop. Asyncio doesn't support | ||
that and the RuntimeError is raised. | ||
So, this context manager patches AsyncHTTPConnection class in a way that all of the | ||
async connections expire immediately and the runtime error is not possible. | ||
Related issues: | ||
https://github.com/comet-ml/opik/issues/1132 | ||
https://github.com/encode/httpx/discussions/2959 | ||
TODO: this function might probably require extra logic for handling the cases | ||
when there is already existing async connection pool with opened connections, but it is | ||
out of scope for now. | ||
""" | ||
try: | ||
original = httpcore.AsyncHTTPConnection.__init__ | ||
|
||
def AsyncHTTPConnection__init__wrapper() -> Callable: | ||
@functools.wraps(original) | ||
def wrapped(*args, **kwargs): # type: ignore | ||
kwargs["keepalive_expiry"] = 0 | ||
return original(*args, **kwargs) | ||
|
||
return wrapped | ||
|
||
httpcore.AsyncHTTPConnection.__init__ = AsyncHTTPConnection__init__wrapper() | ||
yield | ||
finally: | ||
httpcore.AsyncHTTPConnection.__init__ = original |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters