Summary: Pymongo appears to fail silently for no reason in my flask+gevent+uwsgi+nginx app. I would love some pointers on where I should look
I'm a newcomer to web application programming (and to python), please bear with me. I'm porting an app from Heroku to an OpenStack provider, and am finding that code that worked fine on the former fails intermittently and silently on the latter. I wonder if anyone can shed some light on this.
This is the function in question:
emergencies
is a pymongo Collection. This is correctly instantiated.
user_id
is the User id I'm looking for. It's correct.
22 def get_emergency_by_user(user_id):
23 print "going to find emergency by user:"+user_id
24 print emergencies
25 print EmergencyDict.user_id
26 try:
27 emergency = emergencies.find_one({EmergencyDict.user_id: user_id})
28 except:
29 print 'mongo failed'
30 print 'this should appear'
31 print 'emergency - %s' % emergency
32 return emergency
Here is the output from the function (line numbers added for easy reference):
Failure Case
23 going to find emergency by user:UnitTestScript
24 Collection(Database(Connection('[redacted]', [redacted]), u'[redacted]'), u'emergencies')
25 userid
So I can see that line 23 through 25 work fine, and I assume that line 27 is called. But I get nothing below that. Neither line 29 (the except:
case) nor line 30 ever run.
The strangest thing is that there are times during the day when this isn't a problem at all, and it works perfectly. In these cases, it looks more like this (line numbers added for easy reference):
Success Case
23 going to find emergency by user:UnitTestScript
24 Collection(Database(Connection('[redacted]', [redacted]), u'[redacted]'), u'emergencies')
25 userid
30 this should appear
31 {'_obj'...[a bunch of json representing the correct document]...'}
I haven't been able to isolate anything makes it work though. It's maddening, and I don't know where to look next.
some things I have tried
I have read some articles that suggest that I need to have the from gevent import monkey; monkey.patch_all()
line in my imports; I have done this.
I have also read that you can't use uwsgi+gevent with multiple threads, so my uwsgi is configured with 1 thread.
tl;dr Pymongo appears to fail silently for no reason in my flask+gevent+uwsgi+nginx app. I would love some pointers on where I should look
I just remembered how we solved this. The version of pymongo in the requirements.txt was old. I updated it to the newest version, and it was fine after that.