我的堆栈是 uWSGI 1.2.2、bottle 和 gevent 1.0b2。我正在尝试执行以下异步操作。
1)尽可能快地提供像素图像并关闭连接 2)然后我将查询字符串传递给一个函数,以便我可以使用 gevent 将数据异步写入 redis 3)根据http://projects.unbit上的 uwsgi 文档.it/uwsgi/wiki/Gevent我似乎做对了。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()