简体   繁体   中英

Async function call with Tornado Python

I'm trying to make a simple async call, using gen.coroutine function of Tornado. This is my current code:

from tornado import gen
import tornado.ioloop
import tornado.web


class MainHandler(tornado.web.RequestHandler):

    @gen.coroutine
    def get(self):
        q = self.get_argument('query')
        print q
        response = yield self.process(q)
        self.write(response)

    @gen.coroutine
    def process(self, query):
        # just a long loop
        for i in range(int(query)*100):
            for j in range(i):
                a = 10*10*10*10*10*10
        return {'processed': True}


def make_app():
    return tornado.web.Application([
        (r"/search", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    port = 8888
    print "listening on port: ", port
    app.listen(port)
    tornado.ioloop.IOLoop.current().start()

However, it is not behaving in an async manner. What am I doing wrong in this?

Your "process" method does only calculation, so it never provides Tornado's event loop an opportunity to work on other tasks while "process" is running. Tornado can interleave concurrent network operations, however, it cannot run Python code in parallel. To parallelize a function like your "process" method requires multiple Python subprocesses.

Your function is blocking the event loop and no other tasks can be handled until either the process() function completes or it relinquishes control back to the event loop. For situations like this, you can use simply yield None (it used to be yield gen.moment ) to take a break and let the event loop run other tasks then resume processing. Example:

@gen.coroutine
def process(self, query):
    for i in range(int(query)*100):
        for j in range(i):
            a = 10*10*10*10*10*10
            if j % 500 == 0:
                yield None    # yield the inner loop

        if i % 500 == 0:
            yield None    # yield outer loop

    return {'processed': True}

Hopefully this helps you achieve your desired level of concurrency.

References

http://www.tornadoweb.org/en/stable/gen.html#tornado.gen.moment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM