Sending JSON from Tornado to client

14,463

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)) 
Share:
14,463
user94628
Author by

user94628

Updated on June 28, 2022

Comments

  • user94628
    user94628 almost 2 years

    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))?