这是一个愚蠢的 websocket 时钟的有趣之处:
class StupidClock(websocket.WebSocketHandler):
clients = {}
@web.asynchronous
@gen.coroutine
def open(self):
self.is_open = True
def enqueue(callback=None):
self.__class__.clients[self] = callback
while self.is_open:
# This is the most interesting part!
msg = yield gen.Task(enqueue)
self.write_message(msg)
def on_close(self):
self.is_open = False
del self.__class__.clients[self]
@classmethod
def periodic_update(cls):
msg = time.time()
# copy and clear before iterating to avoid infinite growth!
clients = cls.clients.copy()
cls.clients.clear()
for obj, callback in clients.items():
if obj.is_open:
callback(msg)
# all the routing and application setup omitted...
loop = ioloop.IOLoop.instance()
cb = ioloop.PeriodicCallback(StupidClock.periodic_callback, 1,
io_loop=loop)
cb.start()
loop.start()
所以我的问题是关于解构这个陈述:
msg = yield gen.Task(enqueue)
从文档中,它与以下内容相同:
result = yield gen.Task(func, args)
# is the same as
func(args, callback=(yield gen.Callback(key)))
result = yield gen.Wait(key)
我很清楚第一种形式(只有一个yield
表达式)发生了什么,但为什么我必须将控制权交给 Tornado 来创建一个gen.Callback
对象?
一个yield
表达式如何等同于两个yield
表达式?不是必须控制两次对 Tornado 的让步吗?然而,在gen.Task
表格中,我只让步一次!