Commit f9cca5bb7a03b8bdd4f77426f3e7e96eb2ef0b68
1 parent
46132ce0
Exists in
master
and in
1 other branch
- 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,17 +7,15 @@ import base64 | ||
7 | import uuid | 7 | import uuid |
8 | import logging.config | 8 | import logging.config |
9 | import argparse | 9 | import argparse |
10 | -from concurrent.futures import ThreadPoolExecutor | ||
11 | import mimetypes | 10 | import mimetypes |
12 | import signal | 11 | import signal |
12 | +import asyncio | ||
13 | 13 | ||
14 | # user installed libraries | 14 | # user installed libraries |
15 | import tornado.ioloop | 15 | import tornado.ioloop |
16 | import tornado.web | 16 | import tornado.web |
17 | import tornado.httpserver | 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 | # this project | 20 | # this project |
23 | from learnapp import LearnApp | 21 | from learnapp import LearnApp |
@@ -79,11 +77,14 @@ class LoginHandler(BaseHandler): | @@ -79,11 +77,14 @@ class LoginHandler(BaseHandler): | ||
79 | self.render('login.html', error='') | 77 | self.render('login.html', error='') |
80 | 78 | ||
81 | # @gen.coroutine | 79 | # @gen.coroutine |
82 | - def post(self): | 80 | + async def post(self): |
83 | uid = self.get_body_argument('uid') | 81 | uid = self.get_body_argument('uid') |
84 | pw = self.get_body_argument('pw') | 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 | self.set_secure_cookie("user", str(uid), expires_days=30) | 88 | self.set_secure_cookie("user", str(uid), expires_days=30) |
88 | self.redirect(self.get_argument("next", "/")) | 89 | self.redirect(self.get_argument("next", "/")) |
89 | else: | 90 | else: |
@@ -179,14 +180,13 @@ class FileHandler(BaseHandler): | @@ -179,14 +180,13 @@ class FileHandler(BaseHandler): | ||
179 | chunk = f.read(self.chunk_size) | 180 | chunk = f.read(self.chunk_size) |
180 | while chunk: | 181 | while chunk: |
181 | try: | 182 | try: |
182 | - self.write(chunk) # write the cunk to response | 183 | + self.write(chunk) # write the chunk to response |
183 | await self.flush() # flush the current chunk to socket | 184 | await self.flush() # flush the current chunk to socket |
184 | except iostream.StreamClosedError: | 185 | except iostream.StreamClosedError: |
185 | break # client closed the connection | 186 | break # client closed the connection |
186 | finally: | 187 | finally: |
187 | del chunk | 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 | chunk = f.read(self.chunk_size) | 190 | chunk = f.read(self.chunk_size) |
191 | 191 | ||
192 | 192 | ||
@@ -194,8 +194,6 @@ class FileHandler(BaseHandler): | @@ -194,8 +194,6 @@ class FileHandler(BaseHandler): | ||
194 | # respond to AJAX to get a JSON question | 194 | # respond to AJAX to get a JSON question |
195 | # ---------------------------------------------------------------------------- | 195 | # ---------------------------------------------------------------------------- |
196 | class QuestionHandler(BaseHandler): | 196 | class QuestionHandler(BaseHandler): |
197 | - executor = ThreadPoolExecutor() # max_workers=5*n_cpus? | ||
198 | - | ||
199 | templates = { | 197 | templates = { |
200 | 'checkbox': 'question-checkbox.html', | 198 | 'checkbox': 'question-checkbox.html', |
201 | 'radio': 'question-radio.html', | 199 | 'radio': 'question-radio.html', |
@@ -212,12 +210,6 @@ class QuestionHandler(BaseHandler): | @@ -212,12 +210,6 @@ class QuestionHandler(BaseHandler): | ||
212 | # 'alert': '', FIXME | 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 | # --- get question to render | 213 | # --- get question to render |
222 | @tornado.web.authenticated | 214 | @tornado.web.authenticated |
223 | def get(self): | 215 | def get(self): |
@@ -255,7 +247,10 @@ class QuestionHandler(BaseHandler): | @@ -255,7 +247,10 @@ class QuestionHandler(BaseHandler): | ||
255 | answer = answer[0] | 247 | answer = answer[0] |
256 | 248 | ||
257 | # check answer in another thread (nonblocking) | 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 | question = self.learn.get_student_question(user) | 254 | question = self.learn.get_student_question(user) |
260 | 255 | ||
261 | if grade <= 0.999: # wrong answer | 256 | if grade <= 0.999: # wrong answer |
@@ -277,7 +272,8 @@ class QuestionHandler(BaseHandler): | @@ -277,7 +272,8 @@ class QuestionHandler(BaseHandler): | ||
277 | }) | 272 | }) |
278 | else: # right answer, get next question in the topic | 273 | else: # right answer, get next question in the topic |
279 | template = self.templates[question['type']] | 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 | self.write({ | 277 | self.write({ |
282 | 'method': 'new_question', | 278 | 'method': 'new_question', |
283 | 'params': { | 279 | 'params': { |