I am new to tornado.
It's exciting about the part of Coroutines.
So i try to convert a blocking function into a non-blocking at first.
@tornado.concurrent.return_future
def calculate(callback):
start_time = time.time()
res = urllib2.urlopen("https://www.google.com/")
print time.time()-start_time
callback(res)
class MainHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
start_time = time.time()
res = yield [calculate(), calculate()]
print time.time()-start_time
But i got:
1.41436505318
1.38487792015
2.80179595947
It's I/O bound, so i guess the total time spent should be approximate to the longer one time spent(1.41436505318). But it seems to be blocking.
So i am wondering what's going wrong?How can i convert a blocking function into a non-blocking function?
return_future
doesn't make a function non-blocking; it takes a non-blocking function that uses callbacks and makes it coroutine-friendly.
The only way to make a blocking function non-blocking without making deep changes to it is to run it in another thread or process, as with a ThreadPoolExecutor
.
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.