2222import weakref
2323from typing import TYPE_CHECKING , Any , Mapping , Optional , cast
2424
25- from pymongo import common
25+ from pymongo import common , periodic_executor
2626from pymongo ._csot import MovingMinimum
27- from pymongo .asynchronous import periodic_executor
28- from pymongo .asynchronous .periodic_executor import _shutdown_executors
2927from pymongo .errors import NetworkTimeout , NotPrimaryError , OperationFailure , _OperationCancelled
3028from pymongo .hello import Hello
3129from pymongo .lock import _create_lock
3230from pymongo .logger import _SDAM_LOGGER , _debug_log , _SDAMStatusMessage
31+ from pymongo .periodic_executor import _shutdown_executors
3332from pymongo .pool_options import _is_faas
3433from pymongo .read_preferences import MovingAverage
3534from pymongo .server_description import ServerDescription
@@ -76,7 +75,7 @@ async def target() -> bool:
7675 await monitor ._run () # type:ignore[attr-defined]
7776 return True
7877
79- executor = periodic_executor .PeriodicExecutor (
78+ executor = periodic_executor .AsyncPeriodicExecutor (
8079 interval = interval , min_interval = min_interval , target = target , name = name
8180 )
8281
@@ -112,9 +111,9 @@ async def close(self) -> None:
112111 """
113112 self .gc_safe_close ()
114113
115- def join (self , timeout : Optional [int ] = None ) -> None :
114+ async def join (self , timeout : Optional [int ] = None ) -> None :
116115 """Wait for the monitor to stop."""
117- self ._executor .join (timeout )
116+ await self ._executor .join (timeout )
118117
119118 def request_check (self ) -> None :
120119 """If the monitor is sleeping, wake it soon."""
@@ -139,7 +138,7 @@ def __init__(
139138 """
140139 super ().__init__ (
141140 topology ,
142- "pymongo_server_monitor_thread " ,
141+ "pymongo_server_monitor_task " ,
143142 topology_settings .heartbeat_frequency ,
144143 common .MIN_HEARTBEAT_INTERVAL ,
145144 )
@@ -250,7 +249,7 @@ async def _check_server(self) -> ServerDescription:
250249 except (OperationFailure , NotPrimaryError ) as exc :
251250 # Update max cluster time even when hello fails.
252251 details = cast (Mapping [str , Any ], exc .details )
253- self ._topology .receive_cluster_time (details .get ("$clusterTime" ))
252+ await self ._topology .receive_cluster_time (details .get ("$clusterTime" ))
254253 raise
255254 except ReferenceError :
256255 raise
@@ -434,7 +433,7 @@ def __init__(self, topology: Topology, topology_settings: TopologySettings, pool
434433 """
435434 super ().__init__ (
436435 topology ,
437- "pymongo_server_rtt_thread " ,
436+ "pymongo_server_rtt_task " ,
438437 topology_settings .heartbeat_frequency ,
439438 common .MIN_HEARTBEAT_INTERVAL ,
440439 )
@@ -531,4 +530,5 @@ def _shutdown_resources() -> None:
531530 shutdown ()
532531
533532
534- atexit .register (_shutdown_resources )
533+ if _IS_SYNC :
534+ atexit .register (_shutdown_resources )
0 commit comments