client_session
– Logical sessions for sequential operations¶
Logical sessions for ordering sequential operations.
Requires MongoDB 3.6.
New in version 3.6.
Causally Consistent Reads¶
with client.start_session(causal_consistency=True) as session:
collection = client.db.collection
collection.update_one({'_id': 1}, {'$set': {'x': 10}}, session=session)
secondary_c = collection.with_options(
read_preference=ReadPreference.SECONDARY)
# A secondary read waits for replication of the write.
secondary_c.find_one({'_id': 1}, session=session)
If causal_consistency is True (the default), read operations that use the session are causally after previous read and write operations. Using a causally consistent session, an application can read its own writes and is guaranteed monotonic reads, even when reading from replica set secondaries.
Classes¶
-
class
pymongo.client_session.
ClientSession
(client, server_session, options, authset)¶ A session for ordering sequential operations.
-
advance_cluster_time
(cluster_time)¶ Update the cluster time for this session.
Parameters: - cluster_time: The
cluster_time
from another ClientSession instance.
- cluster_time: The
-
advance_operation_time
(operation_time)¶ Update the operation time for this session.
Parameters: - operation_time: The
operation_time
from another ClientSession instance.
- operation_time: The
-
client
¶ The
MongoClient
this session was created from.
-
cluster_time
¶ The cluster time returned by the last operation executed in this session.
-
end_session
()¶ Finish this session.
It is an error to use the session or any derived
Database
,Collection
, orCursor
after the session has ended.
-
has_ended
¶ True if this session is finished.
-
operation_time
¶ The operation time returned by the last operation executed in this session.
-
options
¶ The
SessionOptions
this session was created with.
-
session_id
¶ A BSON document, the opaque server session identifier.
-
-
class
pymongo.client_session.
SessionOptions
(causal_consistency=True)¶ Options for a new
ClientSession
.Parameters: - causal_consistency (optional): If True (the default), read operations are causally ordered within the session.
-
causal_consistency
¶ Whether causal consistency is configured.