Search code examples
pythonreturntornadoyield

Python BadYieldError: yielded unknown object HTTPError('HTTP 599: Connection closed',)


I want to know why in this function:

@tornado.gen.engine
def check_status_changes(netid, sensid):

    como_url = "".join(['http://131.114.52:44444/ztc?netid=', str(netid), \
               '&sensid=', str(sensid), '&start=-5s&end=-1s'])

    http_client = AsyncHTTPClient()
    response = yield tornado.gen.Task(http_client.fetch, como_url)

    if response.error:
        raise Exception(response.error)

when there's response.error, I obtain the title error... what I have to yield in order to catch the returned value in another function?

I would after do something like:

try:
        periodic = tornado.ioloop.PeriodicCallback(check_status_changes(netid, sensid), 5000)
        value = periodic.start()
        print("Secondo")
        print value
    except:
        print("Quarto")
        periodic.stop()
        self.finish()
        return
    else:

I don't know... I would just compare the returned value with another....

Thank you.


Solution

  • That function has a gen.engine decorator, you can't return a value from inside it (not tornado related, you can't return a value inside a generator).

    If you are trying to get a value out of that function -provided you are calling that on IOLoop-, that function should have a callback (callable) keyword argument as such:

    @tornado.gen.engine
    def check_status_changes(netid, sensid, callback=None):
        response = yield tornado.gen.Task(do_your_thing)
        if response.error:
            raise response.error
        callback(response.body)  # this is how you make the value available to the
                                 # caller; response.body is HTTPResponse specific iirc
    

    Now you can call this function somewhere else as such:

    # somewhere in a gen.engine decorated async method
    body = yield tornado.gen.Task(check_status_changes, netid, sensid)