user94628
user94628

Reputation: 3731

Sending JSON from Tornado to client

I have received and processed data received from the client and want to send back the response in a JSON format, however the client hasn't received it. My code is:

def do_find_one(self,live):
    Info = {} 
    isAvailable = str
    count=db.userInfo.find( {'Username': live}).count()
    if  count > 0:
        isAvailable = False
        Info['valid']=isAvailable
        print False, count
        print Info
    else:
        isAvailable = True
        print True, count
    self.write(json.dumps(Info, default=json_util.default)) 

class CheckerHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def post(self):
        pi1 = self.get_argument('display')        
        do_find_one(self,pi1)

Is there a problem with self.write(json.dumps(Info, default=json_util.default))?

Upvotes: 3

Views: 14760

Answers (1)

dano
dano

Reputation: 94961

When you use the tornado.web.asynchronous decorator, you need to call self.finish() at the end of your handler fror the response to be sent to the client:

class CheckerHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def post(self):
        pi1 = self.get_argument('display')        
        do_find_one(self,pi1)
        self.finish()  # Without this the client's request will hang

    def do_find_one(self,live):
        Info = {} 
        isAvailable = str
        count=db.userInfo.find( {'Username': live}).count()
        if  count > 0:
            isAvailable = False
            Info['valid']=isAvailable
            print False, count
            print Info
        else:
            isAvailable = True
            print True, count
        self.write(json.dumps(Info, default=json_util.default)) 

Upvotes: 8

Related Questions