Reputation: 359
So I have this method that is called with an http.get call.
@app.route('/showSQL')
def get_sql():
spid = request.args['spid']
instance = request.args['instance']
user = creds.creds['bh1']['user']
pw = creds.creds['bh1']['pass']
print 'calling show sql', spid, instance
conn = pymssql.connect(instance, user, pw)
cursor = conn.cursor(as_dict=True)
cmd = 'EXEC master..getSpidSQL ' + str(spid)
cursor.execute(cmd)
sql_out = None
for row in cursor:
sql_out = row['EventInfo']
return sql_out
I have read that this error comes up with multithreading and alike, but there is nothing complicated here. The error is being thrown for the first line of this method: 'spid = request.args['spid']'
>>> longrunners.get_sql()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/tools/dba/longrunners/longrunners.py", line 48, in get_sql
spid = request.args['spid']
File "/usr/local/lib/python2.7/site-packages/werkzeug/local.py", line 343, in __getattr__
return getattr(self._get_current_object(), name)
File "/usr/local/lib/python2.7/site-packages/werkzeug/local.py", line 302, in _get_current_object
return self.__local()
File "/usr/local/lib/python2.7/site-packages/flask/globals.py", line 20, in _lookup_req_object
raise RuntimeError('working outside of request context')
RuntimeError: working outside of request context
I have other methods that are successfully accessing the request object, and have verified that I am in fact sending a param from the browser called 'spid'.
Any ideas of what could be causing this? I've looked through a bunch of other questions and looked on the web, but found nothing that dealt with this particular situation.
Thanks!
Upvotes: 4
Views: 7568
Reputation: 4987
Your get_sql
function is a Flask route callback. It expects to be in a request context.
When this function is called with an HTTP request, your request
will be accessible.
If you want to call it from other contexts, decouple the request and the other parts of your function.
@app.route('/showSQL')
def get_sql_callback():
spid = request.args['spid']
instance = request.args['instance']
return get_sql(spid, instance)
def get_sql(spid, instance):
user = creds.creds['bh1']['user']
pw = creds.creds['bh1']['pass']
print 'calling show sql', spid, instance
conn = pymssql.connect(instance, user, pw)
cursor = conn.cursor(as_dict=True)
cmd = 'EXEC master..getSpidSQL ' + str(spid)
cursor.execute(cmd)
sql_out = None
for row in cursor:
sql_out = row['EventInfo']
return sql_out
Now, you can call it from shell or access it as an HTTP request.
Upvotes: 3