Pymongo threading error while connecting to remote server from google app engine
Question: 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/runtime.py", line 152, in HandleRequest
        error)
      File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", 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/wsgi.py", 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/wsgi.py", line 299, in _LoadHandler
        handler, path, err = LoadObject(self._handler)
      File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
        obj = __import__(path[0])
      File "/base/data/home/apps/s~appname-frontend/1.386781073242991356/main.py", line 2, in <module>
        from dbHandler import get, update
      File "/base/data/home/apps/s~appname-frontend/1.386781073242991356/dbHandler.py", line 9, in <module>
        client = MongoClient(serveraddress, 27017)
      File "/base/data/home/apps/s~appname-frontend/1.386781073242991356/pymongo/mongo_client.py", line 372, in __init__
        executor.open()
      File "/base/data/home/apps/s~appname-frontend/1.386781073242991356/pymongo/periodic_executor.py", line 64, in open
        thread.start()
      File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/threading.py", line 505, in start
        _start_new_thread(self.__bootstrap, ())
      File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/runtime.py", line 82, in StartNewThread
        return base_start_new_thread(Run, ())
    


Answer: 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 

