hi,all:
when i use tornado.uwe.asynchronous, i wrote a slice as below:
@authenticated
@tornado.web.removeslash
@tornado.web.asynchronous
def put(self):
status = ErrorCode.SUCCESS
try:
name = 'jia'
data = DotDict(json_decode(self.request.body))
key = data.car_sets['key']
value = data.car_sets['value']
print 'outer_dir()', dir()
except Exception as e:
logging.exception("Set terminal failed. Exception: %s", e.args)
status = ErrorCode.SERVER_BUSY
self.write_ret(status)
IOLoop.instance().add_callback(self.finish)
return
def _on_finish(response):
print 'inner_dir()', dir()
status = ErrorCode.SUCCESS
print 'test name', name
response = json_decode(response)
print 'res', response
if True: #response['success'] == 0:
sql_cmd = 'UPDATE T_TERMINAL_INFO_W SET ' + key + ' = %s'
print 'sql_cmd', sql_cmd
self.db.execute(sql_cmd, value)
else:
status = ErrorCode.SERVER_BUSY
self.write_ret(status)
IOLoop.instance().add_callback(self.finish)
args = DotDict(seq=SeqGenerator.next(self.db),
cid=self.current_user.cid)
args[key] = value
GFSenderHelper.async_forward(GFSenderHelper.URLS.TERMINAL, args,
_on_finish)
then, we can get the output:
outer dir ['self', 'status','name','data','key','value']
inner dir ['self', 'response']
if we delete ' status = ErrorCode.SUCCESS' in _on_finish(), we will encounter with below:
UnboundLocalError: local variable 'status' referenced before assignment
now, my question is :
why in _on_finish method, i can not use the 'status' defined in put method??
# NOTE:
we all know that: in python, the inner method can invoke the variable defined in outer method easily.
def test_out():
name = 'jia'
age = 20
status = 0
print 'outer_dir', dir()
def test_inner():
name = 'jiaxiaolei'
print name , age, status
print 'dir', dir()
test_inner()
test_out()
# output:
outer_dir ['age', 'name', 'status']
dir ['age', 'name', 'status']
any response will be appreciated.
-- Jia Xiaolei