0

我的烧瓶应用程序中有一个奇怪的错误。问题是下面的代码在 Flask 开发服务器中运行良好,但在生产服务器(Uwsgi+Nginx)中失败。该代码的目的是从 mongoDb 检索整个集合并将其内容作为 JSON 提供。

我没有使用 virtualenv,因为我在虚拟机下工作,而且我没有在该机器上运行任何其他项目。所以堆栈是这样的:

Python Flask MongoDb->MongoEngine Uwsgi NginX

我有两个模型:

公司一份:

class Company(Document):
    name = StringField(max_length=50)
    ....

一个用于活动:

class Campaign(Document):
    name = StringField(min_length=8)
    company = ReferenceField(Company)
    ....

路由是这样完成的:

@app.route('/getjson', methods=['GET', 'POST'])
    def getJson():
        if request.method == 'GET':
        collection = request.args.get('collection')
        if collection == 'Campaign':
            return jsonify (getCampaign())

getCampaign() 函数是这样的:

def getCampaign():
    theCollection = Campaign.objects.all()
    theDict = {}
    for obj in x:
        dict_model = {
            obj.name:
            {
            'company': obj.company.name,
            ...
            }
    theDict.update(dict_model)
return theDict

这会产生一个格式良好的 Json,其中“application/json”作为 Content-Type。问题是当在生产服务器上尝试相同的代码时,它会惨遭失败并抛出以下错误!

    Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1518, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1506, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1504, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1264, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1262, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1248, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "./mobuy.py", line 58, in getJson
    return jsonify (getCampaign())
  File "./jsonOut.py", line 36, in getCampaign
    'company': obj.company.name,
  File "/usr/local/lib/python2.6/dist-packages/bson/dbref.py", line 88, in __getattr__
     raise AttributeError(key)
  AttributeError: name

运行 " 'company': "%s" % obj.dict " 会抛出这个结果:

"company":"{'_created': False, '_data': {'status': 3, 'startDate':
datetime.datetime(2012, 5, 23, 20, 22, 28, 42000), None: ObjectId('4fbd4704f65b813c5900000d'), 
'EndDate': datetime.datetime(2012, 5, 23, 20, 22, 28, 42000), 
'name': u'Campaign13', 'max_claimed': 39, 'text': u'a discount', 
'company': DBRef(u'company', ObjectId('4fbd36f2f65b813869000008')),
'image': u'http://www.Company8.com/image.jpg', 'Shops': [],
 'category': 5, 'id': None, 'coupons': []},
 '_id': ObjectId('4fbd4704f65b813c5900000d'), '_changed_fields': [],
 '_initialised': True}",

帮助!为什么这在开发服务器中工作而不在 Uwsgi 中工作?

4

3 回答 3

0

Tty 将 --lazy 添加到您的 uWSGI 选项中。也许您的 mongodb 驱动程序对 fork() 不友好。

于 2012-05-24T17:05:41.923 回答
0

您在开发中的 mongoengine 和 uwsgi / nginx 中是否有相同的版本?

你能试试这个:

Campaign.objects.all().select_related()
于 2012-05-24T17:45:09.503 回答
0

我找到的解决方案是,我绕过问题,直接用pymongo查询数据库

#init pymongo dor dbfer quering as uwsgi fails miserably
connection = Connection()
db = connection.mobuy_test
company_collection = db.company
campaign_collection = db.campaign
for obj in Campaign.objects.all(): #the mongoengine queryset loop
    theId = campaign_collection.find_one({'_id':obj.id})['company'].id
    companyName = company_collection.find_one({'_id':theId})['name']
    dict_model = {
                  'campaign_name': obj.name,
                  'campaign_company':companyName
                   ...
                  }

不确定我是否正确使用了 pymongo 查询,但这目前有效,唯一的问题是,它很脏。

于 2012-05-26T11:52:01.727 回答