PYTHON-4537 - Use selector asyncio loop on windows tests (#1748)

This commit is contained in:
Noah Stapp 2024-07-26 09:49:28 -07:00 committed by GitHub
parent afd0b6f84c
commit cb89061627
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 42 additions and 2 deletions

View File

@ -65,7 +65,17 @@ class PeriodicExecutor:
return f"<{self.__class__.__name__}(name={self._name}) object at 0x{id(self):x}>"
def _run_async(self) -> None:
asyncio.run(self._run()) # type: ignore[func-returns-value]
# The default asyncio loop implementation on Windows
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
# We explicitly use a different loop implementation here to prevent that issue
if sys.platform == "win32":
loop = asyncio.SelectorEventLoop()
try:
loop.run_until_complete(self._run()) # type: ignore[func-returns-value]
finally:
loop.close()
else:
asyncio.run(self._run()) # type: ignore[func-returns-value]
def open(self) -> None:
"""Start. Multiple calls have no effect.

View File

@ -65,7 +65,17 @@ class PeriodicExecutor:
return f"<{self.__class__.__name__}(name={self._name}) object at 0x{id(self):x}>"
def _run_async(self) -> None:
asyncio.run(self._run()) # type: ignore[func-returns-value]
# The default asyncio loop implementation on Windows
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
# We explicitly use a different loop implementation here to prevent that issue
if sys.platform == "win32":
loop = asyncio.SelectorEventLoop()
try:
loop.run_until_complete(self._run()) # type: ignore[func-returns-value]
finally:
loop.close()
else:
asyncio.run(self._run()) # type: ignore[func-returns-value]
def open(self) -> None:
"""Start. Multiple calls have no effect.

View File

@ -79,6 +79,16 @@ from pymongo.synchronous.mongo_client import MongoClient
_IS_SYNC = True
# The default asyncio loop implementation on Windows
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
# We explicitly use a different loop implementation here to prevent that issue
if (
not _IS_SYNC
and sys.platform == "win32"
and asyncio.get_event_loop_policy() == asyncio.WindowsProactorEventLoopPolicy
):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # type: ignore[attr-defined]
class ClientContext:
client: MongoClient

View File

@ -79,6 +79,16 @@ from pymongo.ssl_support import HAVE_SSL, _ssl # type:ignore[attr-defined]
_IS_SYNC = False
# The default asyncio loop implementation on Windows
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
# We explicitly use a different loop implementation here to prevent that issue
if (
not _IS_SYNC
and sys.platform == "win32"
and asyncio.get_event_loop_policy() == asyncio.WindowsProactorEventLoopPolicy
):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # type: ignore[attr-defined]
class AsyncClientContext:
client: AsyncMongoClient