I am struggling with a problem related to pimongo and threads. I have a python application that uses threads. This application executes requests through pymongo, for example db.collection.find_one({'_id': obj_id}). But very often these requests return None, however, an object with this id exists in the database. I cannot reproduce this behavior on my local machine, I do not know why. This only happens in production.
I read this article an article that talks about pymongo, sockets, and threads. But this article describes an old approach that was used in an older version of pymongo. Also, I found this similar issue
I use one instance of the connection in all threads pymongo.MongoClient('mongodb://localhost:27100/my_db').get_default_database(). I thought that No, maybe comes as an answer for another thread, because pymongo uses a socket pool.
For example: thread_1 executes the request and sleep. After this request, the socket returns to pumongo pum. thread_2 wakes up and makes a request, but pymongo gets the socket used by thread_1, and thread_2 gets someone else's response.
This is just an idea. I am not sure if this is true.
What problem can I have?
PS: I use pymongo==3.3.0 and MongoDB 3.2.0
source
share