what happens to variables in tornado coroutines functions?

后端 未结 1 657
温柔的废话
温柔的废话 2021-01-14 14:54

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 UserPostHa         


        
1条回答
  •  既然无缘
    2021-01-14 15:32

    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.

    0 讨论(0)
提交回复
热议问题