first, here is what is said in Pymongo Documentation
By default, PyMongo starts a request for each thread when the thread first runs an operation on Mong
There are a couple of points about this question.
You aren't guaranteed to have read-after-write consistency unless you're using either "safe=true"
, "w=1"
(or greater) or "j=true"
with your write. You can either include these as part of the insert() or update()
commands, or else use set_lasterror_options()
to set these options for the connection, database, or collection that you're using.
If you're allowing reads from secondary nodes, (e.g. a ReadPreference other than PRIMARY), then you will not get read-after-write semantics, but only eventual consistency.
If you are using a ReadPreference of PRIMARY and you're setting the appropriate lasterror options, then you're guaranteed to get read-after-write semantics on all operations that use the same socket, that is, the same thread.
If you're using multiple threads, and you are NOT reading from secondary nodes, then you're guaranteed to get read-after-write consistency as long as you issue the read in the second thread after the write completes in the first thread. You can use standard thread synchronization primitives to assure this.