Skip to content Skip to sidebar Skip to footer

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 M

Solution 1:

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.coroutinedefprocess(self, query):
    for i inrange(int(query)*100):
        for j inrange(i):
            a = 10*10*10*10*10*10if j % 500 == 0:
                yieldNone# yield the inner loopif i % 500 == 0:
            yieldNone# yield outer loopreturn {'processed': True}

Hopefully this helps you achieve your desired level of concurrency.

References

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

Solution 2:

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.

Post a Comment for "Async Function Call With Tornado Python"