Async Function Call With Tornado Python
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.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
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"