Skip to content

Commit 5e3bc65

Browse files
committed
Use run_in_executor for getaddrinfo
1 parent 39ade36 commit 5e3bc65

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

pymongo/asynchronous/helpers.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import asyncio
1919
import builtins
20+
import functools
2021
import socket
2122
import sys
2223
from typing import (
@@ -26,6 +27,7 @@
2627
cast,
2728
)
2829

30+
from pymongo._asyncio_executor import _PYMONGO_EXECUTOR
2931
from pymongo.errors import (
3032
OperationFailure,
3133
)
@@ -70,14 +72,24 @@ async def inner(*args: Any, **kwargs: Any) -> Any:
7072
return cast(F, inner)
7173

7274

73-
async def getaddrinfo(host, port, **kwargs):
75+
async def getaddrinfo(
76+
host: Any, port: Any, **kwargs: Any
77+
) -> list[
78+
tuple[
79+
socket.AddressFamily,
80+
socket.SocketKind,
81+
int,
82+
str,
83+
tuple[str, int] | tuple[str, int, int, int],
84+
]
85+
]:
7486
if not _IS_SYNC:
7587
loop = asyncio.get_running_loop()
76-
return await loop.getaddrinfo( # type: ignore[assignment]
77-
host, port, **kwargs
88+
return await loop.run_in_executor( # type: ignore[return-value]
89+
_PYMONGO_EXECUTOR, functools.partial(socket.getaddrinfo, host, port, **kwargs)
7890
)
7991
else:
80-
return socket.getaddrinfo(host, port, **kwargs) # type: ignore[assignment]
92+
return socket.getaddrinfo(host, port, **kwargs)
8193

8294

8395
if sys.version_info >= (3, 10):

pymongo/synchronous/helpers.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import asyncio
1919
import builtins
20+
import functools
2021
import socket
2122
import sys
2223
from typing import (
@@ -26,6 +27,7 @@
2627
cast,
2728
)
2829

30+
from pymongo._asyncio_executor import _PYMONGO_EXECUTOR
2931
from pymongo.errors import (
3032
OperationFailure,
3133
)
@@ -70,14 +72,24 @@ def inner(*args: Any, **kwargs: Any) -> Any:
7072
return cast(F, inner)
7173

7274

73-
def getaddrinfo(host, port, **kwargs):
75+
def getaddrinfo(
76+
host: Any, port: Any, **kwargs: Any
77+
) -> list[
78+
tuple[
79+
socket.AddressFamily,
80+
socket.SocketKind,
81+
int,
82+
str,
83+
tuple[str, int] | tuple[str, int, int, int],
84+
]
85+
]:
7486
if not _IS_SYNC:
7587
loop = asyncio.get_running_loop()
76-
return loop.getaddrinfo( # type: ignore[assignment]
77-
host, port, **kwargs
88+
return loop.run_in_executor( # type: ignore[return-value]
89+
_PYMONGO_EXECUTOR, functools.partial(socket.getaddrinfo, host, port, **kwargs)
7890
)
7991
else:
80-
return socket.getaddrinfo(host, port, **kwargs) # type: ignore[assignment]
92+
return socket.getaddrinfo(host, port, **kwargs)
8193

8294

8395
if sys.version_info >= (3, 10):

0 commit comments

Comments
 (0)