Pymongo threading error when connecting to the remote server from the Google Application Engine


I have deployed a Flask application on Google App Engine. I am connecting to MongoDB hosted at google compute engine using pymongo.

Here is my snippet:

from pymongo import MongoClient, ASCENDING, DESCENDING

serveraddress = 'my_server_address'
client = MongoClient(serveraddress, 27017)
db = client['MasterData']
MJCollection = db['StoredJsons']
print MJCollection.count()

This gives an output but the process stops: This is the error:

Thread running after request. Creation traceback:
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/", line 152, in HandleRequest
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/", line 329, in HandleRequest
    return WsgiRequest(environ, handler_name, url, post_data, error).Handle()
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/", line 85, in LoadObject
    obj = __import__(path[0])
  File "/base/data/home/apps/s~appname-frontend/1.386781073242991356/", line 2, in <module>
    from dbHandler import get, update
  File "/base/data/home/apps/s~appname-frontend/1.386781073242991356/", line 9, in <module>
    client = MongoClient(serveraddress, 27017)
  File "/base/data/home/apps/s~appname-frontend/1.386781073242991356/pymongo/", line 372, in __init__
  File "/base/data/home/apps/s~appname-frontend/1.386781073242991356/pymongo/", line 64, in open
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/", line 505, in start
    _start_new_thread(self.__bootstrap, ())
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/", line 82, in StartNewThread
    return base_start_new_thread(Run, ())

IMHO this configuration is not going to work. Pymongo MongoClient creates a couple of threads when connected for monitoring purposes etc. GAE wouldn't allow that.

The reason you are geting the exemption on MJCollection.count() and not on client = MongoClient(serveraddress, 27017) is that this is the time MongoClient tries to connect.

Your alternatives are :

  • Use mongoDB's REST API but then you will get penalized on functionality and speed.
  • Move your flask appl to Compute Engine