I am writing a web server in tornado and I wanted to know how does tornado.concurrent.Future works? I have seen the example of AsyncHTTPClient() but if I want to make my own function which has some some IO operations to perform like get the values from memcache how do I make it to work asynchronously?
When I use websocket.WebSocketHandler is the function call write_message() blocking the server? My code is like this below:
`class SocketHandler(websocket.WebSocketHandler):
@gen.coroutine
def getMemcVal(self):
self.write_message("some test value")`
- How does the Future differ from the callbacks?
An example about how async & await
works:
# python
async def example():
response = await request.get(“http://www.google.com”)
result = await response.read()
print(result)
equals to callback
in nodejs:
function example() {
request.get(“http://www.google.com”, function (response){
response.read(function (result) {
console.log(result)
})
})
}
So what is Future
? It is an abstract concept. You think an asynchronous function as an object, and then you think event loop
is scheduling multiple objects like scheduling multiple threads by recognizing await
.
But what is real inside it? Just the same as callback
. It's just a syntactic sugar.