uwsgi,gevent和async循环 - UnboundLocalError:在赋值之前引用的局部变量'query_string'

时间:2012-05-12 19:20:54

标签: python wsgi gevent uwsgi

我的堆栈是uWSGI 1.2.2,瓶子和gevent 1.0b2。我正在尝试执行以下异步。

1)尽可能快地提供像素图像并关闭连接 2)然后我将查询字符串传递给函数,以便我可以使用gevent将数据异步写入redis 3)根据http://projects.unbit.it/uwsgi/wiki/Gevent的uwsgi文档,我似乎做得对。 4)在日志中我收到此错误。我服务像素但是......

Traceback (most recent call last):
  File "/home/ubuntu/workspace/bottleServer.py", line 222, in upixel
    gevent.spawn(pixelRedisWrite,cookie_id,query_string,yield_time)
UnboundLocalError: local variable 'query_string' referenced before assignment
[pid: 28173|app: 0|req: 91/91] 120.28.191.173 () {40 vars in 1909 bytes} [Sat May 12 19:07:24 2012] GET /upixel?bid=eydhdmlkJzogJ2luZm9AYWRtYWdpYy5jby5qcCcsICdjcmlkJzogJzIwNzY3MDczNTE1JywgJ21hYm

所以....为什么会这样?查询字符串就在那里....

以下是我发布uwsgi的方法

sudo /usr/local/bin/uwsgi --loop gevent --socket :3031 --wsgi-file /home/ubuntu/workspace/bottleServer.py --master --async 10 --listen 100 --processes 1 





def pixelRedisWrite(ckid,qs,yield_time):
    pass


@route('/upixel/')
@route('/upixel')
def upixel():
    sw = stopwatch.Timer()

    if request.get_cookie('rtbhui'):
        cookie_id = request.get_cookie('rtbhui')
    else:
        cookie_id=str(uuid4())
        response.set_cookie('rtbhui', cookie_id , max_age=31556952*2, domain='rtb.rtbhui.com')

    cookie_id='test'
    response.content_type = 'image/gif'
    sw.stop()
    yield_time = int(sw.elapsed * 1000)
    if request.query.bid:
        query_string = base64.b64decode(request.query.bid)
        query_string = ast.literal_eval(query_string)
        #pixelRedisWrite(cookie_id,query_string,yield_time)
    yield pixel
    #print 'gggggg',query_string
    gevent.spawn(pixelRedisWrite,cookie_id,query_string,yield_time)


if __name__ == "__main__":
    # Interactive mode
    run(host='localhost', port=8080)
else:
    # Mod WSGI launch
    os.chdir(os.path.dirname(__file__))
    application = default_app()

1 个答案:

答案 0 :(得分:2)

这与gevent或uwsgi无关。您仅在某些情况下(query_string为真时)定义request.query.bid。如果不是,则变量未定义,UnboundLocalError告诉您。