Slide 9
Slide 9 text
How to write Non-block
server?
Call Backs
Coroutine (Decorator)
Yield (Lazy Eval)
def wait_for_messages(self, callback, cursor=None):
if cursor:
new_count = 0
for msg in reversed(self.cache):
if msg["id"] == cursor:
break
new_count += 1
if new_count:
callback(self.cache[-new_count:])
return
self.waiters.add(callback)
!
def cancel_wait(self, callback):
self.waiters.remove(callback)
!
class MessageNewHandler(BaseHandler):
@tornado.web.authenticated
@gen.coroutine
def post(self):
message = {
"id": str(uuid.uuid4()),
"from": self.current_user["first_name"],
"body": self.get_argument("body"),
}
!
cursor = collection.find({'$or': [{'teamA': self.get_argument("body")},{'teamB'
new_message = [self.get_argument("body"),"Matches:"]
withMatch = False
while (yield cursor.fetch_next):
document = cursor.next_object()
withMatch = True
try:
matchDatetime = document.get('data').get('prematch').get('match').get('
except:
try:
matchDatetime = document.get('data').get('match').get('start_time')
except:
pass
!
print matchDatetime
msg_str = matchDatetime+':'+document.get('teamA','')+' v '+ document.get('t
if isinstance(document.get('goalA'), int ):
msg_str = msg_str+' '+str(document.get('goalA',''))+' : '+ str(document
new_message.append(msg_str)
!