RuntimeError:在请求上下文之外工作时方法的第一行中使用了请求 [英] RuntimeError: working outside of request context When request used in first line of method

查看:57
本文介绍了RuntimeError:在请求上下文之外工作时方法的第一行中使用了请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有通过http.get调用来调用的方法.

  @ app.route('/showSQL')def get_sql():spid = request.args ['spid']实例= request.args ['instance']用户= creds.creds ['bh1'] ['用户']pw = creds.creds ['bh1'] ['pass']打印'calling show sql',spid,实例conn = pymssql.connect(instance,user,pw)cursor = conn.cursor(as_dict = True)cmd ='EXEC master..getSpidSQL'+ str(spid)cursor.execute(cmd)sql_out =无对于光标中的行:sql_out = row ['EventInfo']返回sql_out 

我已经阅读到此错误是由多线程和类似问题引起的,但是这里没有什么复杂的问题.该方法的第一行将引发错误:'spid = request.args ['spid']'

 >>>longrunners.get_sql()追溯(最近一次通话):在< module>中的文件< stdin>",第1行,get_sql中的文件"/opt/tools/dba/longrunners/longrunners.py",第48行spid = request.args ['spid']__getattr__中的文件"/usr/local/lib/python2.7/site-packages/werkzeug/local.py",第343行返回getattr(self._get_current_object(),name)_get_current_object中的文件"/usr/local/lib/python2.7/site-packages/werkzeug/local.py",第302行返回self .__ local()_lookup_req_object中的文件"/usr/local/lib/python2.7/site-packages/flask/globals.py",第20行引发RuntimeError('在请求上下文之外工作')RuntimeError:在请求上下文之外工作 

我还有其他方法可以成功访问请求对象,并且已经验证我实际上是从浏览器发送名为"spid"的参数.

关于可能是什么原因的任何想法?我浏览了许多其他问题并浏览了网络,但是没有发现任何可以处理这种特殊情况的东西.

谢谢!

解决方案

您的 get_sql 函数是Flask路由回调.它期望在请求上下文中.

通过HTTP请求调用此函数时,您的请求将可访问.

如果要从其他上下文中调用它,请分离请求和函数的其他部分.

  @ app.route('/showSQL')def get_sql_callback():spid = request.args ['spid']实例= request.args ['instance']返回get_sql(spid,instance)def get_sql(spid,instance):用户= creds.creds ['bh1'] ['用户']pw = creds.creds ['bh1'] ['pass']打印'calling show sql',spid,实例conn = pymssql.connect(instance,user,pw)cursor = conn.cursor(as_dict = True)cmd ='EXEC master..getSpidSQL'+ str(spid)cursor.execute(cmd)sql_out =无对于光标中的行:sql_out = row ['EventInfo']返回sql_out 

现在,您可以从shell调用它,也可以作为HTTP请求访问它.

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!

解决方案

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.

这篇关于RuntimeError:在请求上下文之外工作时方法的第一行中使用了请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆