Commit 41d56522cf719f184135595add656a883c8db79e

Authored by Miguel Barao
1 parent 34778ed8
Exists in master and in 1 other branch dev

check answer is now async

Showing 3 changed files with 17 additions and 16 deletions   Show diff stats
@@ -144,13 +144,13 @@ class StudentKnowledge(object): @@ -144,13 +144,13 @@ class StudentKnowledge(object):
144 # - if answer ok, goes to next question 144 # - if answer ok, goes to next question
145 # - if wrong, counts number of tries. If exceeded, moves on. 145 # - if wrong, counts number of tries. If exceeded, moves on.
146 # ------------------------------------------------------------------------ 146 # ------------------------------------------------------------------------
147 - def check_answer(self, answer): 147 + async def check_answer(self, answer):
148 logger.debug('StudentKnowledge.check_answer()') 148 logger.debug('StudentKnowledge.check_answer()')
149 149
150 q = self.current_question 150 q = self.current_question
151 q['answer'] = answer 151 q['answer'] = answer
152 q['finish_time'] = datetime.now() 152 q['finish_time'] = datetime.now()
153 - grade = q.correct() 153 + grade = await q.correct_async()
154 154
155 logger.debug(f'Grade {grade:.2} ({q["ref"]})') 155 logger.debug(f'Grade {grade:.2} ({q["ref"]})')
156 156
@@ -138,14 +138,14 @@ class LearnApp(object): @@ -138,14 +138,14 @@ class LearnApp(object):
138 # ------------------------------------------------------------------------ 138 # ------------------------------------------------------------------------
139 # checks answer (updating student state) and returns grade. 139 # checks answer (updating student state) and returns grade.
140 # ------------------------------------------------------------------------ 140 # ------------------------------------------------------------------------
141 - def check_answer(self, uid, answer): 141 + async def check_answer(self, uid, answer):
142 knowledge = self.online[uid]['state'] 142 knowledge = self.online[uid]['state']
143 - q = knowledge.check_answer(answer) # also moves to next question 143 + q = await knowledge.check_answer(answer) # also moves to next question
144 logger.info(f'User "{uid}" got {q["grade"]:.2} in question "{q["ref"]}"') 144 logger.info(f'User "{uid}" got {q["grade"]:.2} in question "{q["ref"]}"')
145 topic = knowledge.get_current_topic() 145 topic = knowledge.get_current_topic()
146 146
147 # always save grade of answered question 147 # always save grade of answered question
148 - with self.db_session(autoflush=False) as s: 148 + with self.db_session() as s:
149 s.add(Answer( 149 s.add(Answer(
150 ref=q['ref'], 150 ref=q['ref'],
151 grade=q['grade'], 151 grade=q['grade'],
@@ -155,13 +155,13 @@ class LearnApp(object): @@ -155,13 +155,13 @@ class LearnApp(object):
155 topic_id=topic)) 155 topic_id=topic))
156 logger.debug(f'Saved "{q["ref"]}" into database') 156 logger.debug(f'Saved "{q["ref"]}" into database')
157 157
158 - if knowledge.get_current_question() is None:  
159 - # finished topic, save into database  
160 - logger.info(f'User "{uid}" finished "{topic}"')  
161 - level = knowledge.get_topic_level(topic)  
162 - date = str(knowledge.get_topic_date(topic)) 158 + if knowledge.get_current_question() is None:
  159 + # finished topic, save into database
  160 + logger.info(f'User "{uid}" finished "{topic}"')
  161 + level = knowledge.get_topic_level(topic)
  162 + date = str(knowledge.get_topic_date(topic))
163 163
164 - # with self.db_session(autoflush=False) as s: 164 + with self.db_session() as s:
165 a = s.query(StudentTopic).filter_by(student_id=uid, topic_id=topic).one_or_none() 165 a = s.query(StudentTopic).filter_by(student_id=uid, topic_id=topic).one_or_none()
166 if a is None: 166 if a is None:
167 # insert new studenttopic into database 167 # insert new studenttopic into database
@@ -175,11 +175,10 @@ class LearnApp(object): @@ -175,11 +175,10 @@ class LearnApp(object):
175 a.date = date 175 a.date = date
176 176
177 s.add(a) 177 s.add(a)
178 - logger.debug(f'Saved topic "{topic}" into database')  
179 178
180 -  
181 - if knowledge.get_current_question() is None: 179 + logger.debug(f'Saved topic "{topic}" into database')
182 return 'finished_topic' 180 return 'finished_topic'
  181 +
183 if q['tries'] > 0 and q['grade'] <= 0.999: 182 if q['tries'] > 0 and q['grade'] <= 0.999:
184 return 'wrong' 183 return 'wrong'
185 # elif q['tries'] <= 0 and q['grade'] <= 0.999: 184 # elif q['tries'] <= 0 and q['grade'] <= 0.999:
@@ -275,8 +275,10 @@ class QuestionHandler(BaseHandler): @@ -275,8 +275,10 @@ class QuestionHandler(BaseHandler):
275 answer = answer[0] 275 answer = answer[0]
276 276
277 # check answer in another thread (nonblocking) 277 # check answer in another thread (nonblocking)
278 - action = await asyncio.get_event_loop().run_in_executor(None,  
279 - self.learn.check_answer, user, answer) 278 + # action = await asyncio.get_event_loop().run_in_executor(None,
  279 + # self.learn.check_answer, user, answer)
  280 + action = await self.learn.check_answer(user, answer)
  281 +
280 282
281 # get next question (same, new or None) 283 # get next question (same, new or None)
282 question = self.learn.get_current_question(user) 284 question = self.learn.get_current_question(user)