Skip to content

Commit a6be12a

Browse files
committed
DOCSP-48169: Monitoring page async example
1 parent a235231 commit a6be12a

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

source/includes/monitoring/monitoring.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from pymongo import MongoClient
1+
from pymongo import MongoClient, AsyncMongoClient
22
from pymongo.monitoring import CommandListener, CommandSucceededEvent, ServerListener, \
33
ConnectionPoolListener, ServerHeartbeatStartedEvent, \
44
ConnectionCreatedEvent
55

6-
# start-monitoring
6+
# start-monitoring-listeners
77
class MyCommandListener(CommandListener):
88
def succeeded(self, event: CommandSucceededEvent):
99
print(f"Command {event.command_name} succeeded")
@@ -21,7 +21,14 @@ def connection_created(self, event: ConnectionCreatedEvent):
2121
print(f"Connection {event.connection_id} created")
2222

2323
# Include other event method implementations here
24+
# end-monitoring-listeners
2425

26+
# start-monitoring-client
2527
listeners = [MyCommandListener(), MyServerListener(), MyPoolListener()]
2628
client = MongoClient("<connection URI>", event_listeners=listeners)
27-
# end-monitoring
29+
# end-monitoring-client
30+
31+
# start-monitoring-client-async
32+
listeners = [MyCommandListener(), MyServerListener(), MyPoolListener()]
33+
client = AsyncMongoClient("<connection URI>", event_listeners=listeners)
34+
# end-monitoring-client-async

source/monitoring.txt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,34 @@ connection pool events:
8686

8787
.. literalinclude:: /includes/monitoring/monitoring.py
8888
:language: python
89-
:start-after: start-monitoring
90-
:end-before: end-monitoring
89+
:start-after: start-monitoring-listeners
90+
:end-before: end-monitoring-listeners
9191
:copyable: true
9292

93+
You can then pass these listeners to the ``MongoClient`` constructor to monitor your
94+
application. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the
95+
corresponding code:
96+
97+
.. tabs::
98+
99+
.. tab:: Synchronous
100+
:tabid: sync
101+
102+
.. literalinclude:: /includes/monitoring/monitoring.py
103+
:language: python
104+
:start-after: start-monitoring-client
105+
:end-before: end-monitoring-client
106+
:copyable: true
107+
108+
.. tab:: Asynchronous
109+
:tabid: async
110+
111+
.. literalinclude:: /includes/monitoring/monitoring.py
112+
:language: python
113+
:start-after: start-monitoring-client-async
114+
:end-before: end-monitoring-client-async
115+
:copyable: true
116+
93117
API Documentation
94118
-----------------
95119

0 commit comments

Comments
 (0)