Below is my basic handler. I just want to server an image and after the requeste is complete, I want to write to redis but when I run the below code, the on_finish is not called.
import cyclone.web
import cyclone.redis
from twisted.internet import defer
from twisted.internet import reactor
tt = cyclone.redis.lazyConnectionPool()
class PixelHandler(cyclone.web.RequestHandler):
@cyclone.web.asynchronous
def get(self):
qs = qs_decode(self.request.query)
self.set_header('Content-Type', 'image/gif')
self.write(pixel)
redisPixelWrite(remote_ip)
#self.finish()
def on_finish(self):
t = yield tt.multi()
yield t.set('key', 'value')
r = yield t.commit()
print "commit=", repr(r)
on_finish
will not be called unlessself.finish()
has been called. I'm sure you already know that. The problem in this case is that on_finish() does not supportdefer.inlineCallbacks
(You haven't decorated youron_finish()
method with it, but doing so wouldn't help).I think rewriting
on_finish()
to use plain old deferreds will make this work. Try something like this:A better approach would be to decorate your
get()
method withdefer.inlineCallbacks()
and move the body ofon_finish()
into it.