Commit f9cca5bb7a03b8bdd4f77426f3e7e96eb2ef0b68

Authored by Miguel Barão
1 parent 46132ce0
Exists in master and in 1 other branch dev

- fixed serve.py to work with tornado 5.0

Showing 1 changed file with 15 additions and 19 deletions   Show diff stats
serve.py
... ... @@ -7,17 +7,15 @@ import base64
7 7 import uuid
8 8 import logging.config
9 9 import argparse
10   -from concurrent.futures import ThreadPoolExecutor
11 10 import mimetypes
12 11 import signal
  12 +import asyncio
13 13  
14 14 # user installed libraries
15 15 import tornado.ioloop
16 16 import tornado.web
17 17 import tornado.httpserver
18   -from tornado import template, iostream, gen
19   -from tornado.concurrent import run_on_executor
20   -from tornado.platform.asyncio import to_tornado_future
  18 +from tornado import template, iostream
21 19  
22 20 # this project
23 21 from learnapp import LearnApp
... ... @@ -79,11 +77,14 @@ class LoginHandler(BaseHandler):
79 77 self.render('login.html', error='')
80 78  
81 79 # @gen.coroutine
82   - def post(self):
  80 + async def post(self):
83 81 uid = self.get_body_argument('uid')
84 82 pw = self.get_body_argument('pw')
85 83  
86   - if self.learn.login(uid, pw):
  84 + loop = asyncio.get_event_loop()
  85 + login_ok = await loop.run_in_executor(None, self.learn.login, uid, pw)
  86 +
  87 + if login_ok:
87 88 self.set_secure_cookie("user", str(uid), expires_days=30)
88 89 self.redirect(self.get_argument("next", "/"))
89 90 else:
... ... @@ -179,14 +180,13 @@ class FileHandler(BaseHandler):
179 180 chunk = f.read(self.chunk_size)
180 181 while chunk:
181 182 try:
182   - self.write(chunk) # write the cunk to response
  183 + self.write(chunk) # write the chunk to response
183 184 await self.flush() # flush the current chunk to socket
184 185 except iostream.StreamClosedError:
185 186 break # client closed the connection
186 187 finally:
187 188 del chunk
188   - await gen.sleep(0.000000001) # 1 nanosecond (hack)
189   - # FIXME in the upcomming tornado 5.0 use `await asyncio.sleep(0)` instead
  189 + await asyncio.sleep(0) # 1 nanosecond (hack)
190 190 chunk = f.read(self.chunk_size)
191 191  
192 192  
... ... @@ -194,8 +194,6 @@ class FileHandler(BaseHandler):
194 194 # respond to AJAX to get a JSON question
195 195 # ----------------------------------------------------------------------------
196 196 class QuestionHandler(BaseHandler):
197   - executor = ThreadPoolExecutor() # max_workers=5*n_cpus?
198   -
199 197 templates = {
200 198 'checkbox': 'question-checkbox.html',
201 199 'radio': 'question-radio.html',
... ... @@ -212,12 +210,6 @@ class QuestionHandler(BaseHandler):
212 210 # 'alert': '', FIXME
213 211 }
214 212  
215   - # Blocking function to be run on the executor
216   - @run_on_executor()
217   - def check_answer(self, user, answer):
218   - return self.learn.check_answer(user, answer)
219   -
220   -
221 213 # --- get question to render
222 214 @tornado.web.authenticated
223 215 def get(self):
... ... @@ -255,7 +247,10 @@ class QuestionHandler(BaseHandler):
255 247 answer = answer[0]
256 248  
257 249 # check answer in another thread (nonblocking)
258   - grade = await to_tornado_future(self.check_answer(user, answer))
  250 + loop = asyncio.get_event_loop()
  251 + grade = await loop.run_in_executor(None, self.learn.check_answer, user, answer)
  252 +
  253 + print('grade = ', grade)
259 254 question = self.learn.get_student_question(user)
260 255  
261 256 if grade <= 0.999: # wrong answer
... ... @@ -277,7 +272,8 @@ class QuestionHandler(BaseHandler):
277 272 })
278 273 else: # right answer, get next question in the topic
279 274 template = self.templates[question['type']]
280   - question_html = self.render_string(template, question=question, md=md_to_html)
  275 + question_html = self.render_string(
  276 + template, question=question, md=md_to_html)
281 277 self.write({
282 278 'method': 'new_question',
283 279 'params': {
... ...