Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyMongo - Create MongoClient with connect=False, or create client after forking

I'm developing web app in flask with mongodb (mLab). After deploying it for heroku I have such error:

userWarning: MongoClient opened before fork. Create MongoClient with connect=False, or create client after forking.

I found this documentation but have no idea how to use it with my code. http://api.mongodb.com/python/current/faq.html#using-pymongo-with-multiprocessing

Here is part of my code. Can anyone show me how to create MongoClient with connect=False, or create client after forking?

from flask import Flask, render_template, url_for, request, session, redirect
from flask_pymongo import PyMongo


app = Flask(__name__)

app.config['MONGO_DBNAME'] = 'connect_to_mongo'
app.config['MONGO_URI'] = 'mongodb://admin:[email protected]:45148/xxxxxx'
mongo = PyMongo(app)

@app.route('/companies', methods=['GET'])
def showCompanies():
    if 'userName' not in session:
        return redirect(url_for('start_page'))
    mongoCompanies = mongo.db.companies
    mLabCompanies = [x for x in mongoCompanies.find()]
    return render_template('companies/companies.html', mLabCompanies=mLabCompanies)

Log from heroku:

2017-01-28T09:21:05.106084+00:00 heroku[router]: at=info method=GET path="/" host=hidden-crag-49839.herokuapp.com request_id=305240cf-6cb2-493d-8e07-22fac67c7fd4 fwd="89.75.75.183" dyno=web.1 connect=0ms service=5ms status=200 bytes=678
2017-01-28T09:21:05.107723+00:00 app[web.1]: {address space usage: 400879616 bytes/382MB} {rss usage: 20213760 bytes/19MB} [pid: 11|app: 0|req: 3/3] 10.13.194.229 () {50 vars in 910 bytes} [Sat Jan 28 09:21:05 2017] GET / => generated 598 bytes in 4 msecs (HTTP/1.1 200) 2 headers in 80 bytes (1 switches on core 0)
2017-01-28T09:21:09.940804+00:00 app[web.1]: /app/.heroku/python/lib/python2.7/site-packages/pymongo/topology.py:145: UserWarning: MongoClient opened before fork. Create MongoClient with connect=False, or create client after forking. See PyMongo's documentation for details: http://api.mongodb.org/python/current/faq.html#pymongo-fork-safe>
2017-01-28T09:21:09.940813+00:00 app[web.1]:   "MongoClient opened before fork. Create MongoClient "
2017-01-28T09:21:39.931057+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/login" host=hidden-crag-49839.herokuapp.com request_id=da7bb67b-9f1c-4e1c-84b6-cdfd131008dc fwd="89.75.75.183" dyno=web.1 connect=0ms service=30000ms status=503 bytes=0
2017-01-28T09:21:40.136006+00:00 app[web.1]: [2017-01-28 09:21:40,132] ERROR in app: Exception on /login [POST]
2017-01-28T09:21:40.136016+00:00 app[web.1]: Traceback (most recent call last):
2017-01-28T09:21:40.136017+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
2017-01-28T09:21:40.136018+00:00 app[web.1]:     response = self.full_dispatch_request()
2017-01-28T09:21:40.136019+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
2017-01-28T09:21:40.136020+00:00 app[web.1]:     rv = self.handle_user_exception(e)
2017-01-28T09:21:40.136021+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
2017-01-28T09:21:40.136021+00:00 app[web.1]:     reraise(exc_type, exc_value, tb)
2017-01-28T09:21:40.136022+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
2017-01-28T09:21:40.136022+00:00 app[web.1]:     rv = self.dispatch_request()
2017-01-28T09:21:40.136023+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
2017-01-28T09:21:40.136024+00:00 app[web.1]:     return self.view_functions[rule.endpoint](**req.view_args)
2017-01-28T09:21:40.136024+00:00 app[web.1]:   File "./app.py", line 32, in login
2017-01-28T09:21:40.136025+00:00 app[web.1]:     login_user = users.find_one({'userName' : request.form['loginUsername']})
2017-01-28T09:21:40.136026+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/pymongo/collection.py", line 1102, in find_one
2017-01-28T09:21:40.136026+00:00 app[web.1]:     for result in cursor.limit(-1):
2017-01-28T09:21:40.136027+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/pymongo/cursor.py", line 1114, in next
2017-01-28T09:21:40.136028+00:00 app[web.1]:     if len(self.__data) or self._refresh():
2017-01-28T09:21:40.136029+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/pymongo/cursor.py", line 1036, in _refresh
2017-01-28T09:21:40.136030+00:00 app[web.1]:     self.__collation))
2017-01-28T09:21:40.136030+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/pymongo/cursor.py", line 873, in __send_message
2017-01-28T09:21:40.136031+00:00 app[web.1]:     **kwargs)
2017-01-28T09:21:40.136032+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/pymongo/mongo_client.py", line 888, in _send_message_with_response
2017-01-28T09:21:40.136033+00:00 app[web.1]:     server = topology.select_server(selector)
2017-01-28T09:21:40.136034+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/pymongo/topology.py", line 214, in select_server
2017-01-28T09:21:40.136034+00:00 app[web.1]:     address))
2017-01-28T09:21:40.136035+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/pymongo/topology.py", line 189, in select_servers
2017-01-28T09:21:40.136035+00:00 app[web.1]:     self._error_message(selector))
2017-01-28T09:21:40.136036+00:00 app[web.1]: ServerSelectionTimeoutError: No servers found yet
2017-01-28T09:21:40.140805+00:00 app[web.1]: {address space usage: 401403904 bytes/382MB} {rss usage: 20729856 bytes/19MB} [pid: 11|app: 0|req: 4/4] 10.13.194.229 () {60 vars in 1125 bytes} [Sat Jan 28 09:21:09 2017] POST /login => generated 291 bytes in 30207 msecs (HTTP/1.1 500) 2 headers in 84 bytes (1 switches on core 0)
like image 233
Nick493 Avatar asked Jan 28 '17 01:01

Nick493


1 Answers

You can pass in the connect argument:

app.config['MONGO_CONNECT'] = False

to enable the behavior outlined in the comments above.

I'm currently researching if this should be the default behavior and I will update the library accordingly if I'm able to determine that.

like image 104
Randall Hunt Avatar answered Sep 26 '22 03:09

Randall Hunt