I'm new to the concept of non-blocking IO, and there is something i'm having trouble understanding - about coroutines. consider this code:
class UserPostHandler(RequestHandler):
@gen.coroutine
def get(self):
var = 'some variable'
data = json.loads(self.request.body)
yield motor_db.users.insert({self.request.remote_ip: data})#asynch non blocking db insert call
#success
self.set_status(201)
print var
when the get
function is called, it creates the string var
. what happens to this variable when the function waits for the motor.insert
to complete? To my understanding "non blocking" implies that no thread is waiting for the IO call to complete, and no memory is being used while waiting. So where is the value of var
stored? how is it accessible when the execution resumes?
Any help would be appreciated!
The memory for var
is still being used while insert
executes, but the get
function itself is "frozen", which allows other functions to execute. Tornado's coroutines are implemented using Python generators, which allow function execution to be temporarily suspended when a yield
occurs, and then be restarted again (with the function's state preserved) after the yield point. Here's how the behavior is described in the PEP that introduced generators:
If a yield statement is encountered, the state of the function is frozen, and the value [yielded] is returned to .next()'s caller. By "frozen" we mean that all local state is retained, including the current bindings of local variables, the instruction pointer, and the internal evaluation stack: enough information is saved so that the next time .next() is invoked, the function can proceed exactly as if the yield statement were just another external call.
The @gen.coroutine
generator has magic in it that ties into Tornado's event loop, so that the Future
returned by the insert
call is registered with the event loop, allowing the get
generator to be restarted when the insert
call completes.
来源:https://stackoverflow.com/questions/25256461/what-happens-to-variables-in-tornado-coroutines-functions