我正在使用mongodb(mLab)在烧瓶中开发web应用程序。 在为heroku部署之后,我有这样的错误:
userWarning:在fork之前打开MongoClient。使用创建MongoClient connect = False,或者在分叉后创建客户端。
我找到了这个文档,但不知道如何将它与我的代码一起使用。 http://api.mongodb.com/python/current/faq.html#using-pymongo-with-multiprocessing
这是我的代码的一部分。任何人都可以告诉我如何使用connect = False创建MongoClient,或者在分叉后创建客户端吗?
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:xxxxx@ds145148.mlab.com: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)
从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)
答案 0 :(得分:4)
您可以传入connect参数:
app.config['MONGO_CONNECT'] = False
启用上述评论中列出的行为。
我正在研究这是否应该是默认行为,如果我能够确定,我会相应地更新库。