(self, nonblock=False)
| 173 | self._on_session_close() |
| 174 | |
| 175 | def _cleanup(self, nonblock=False): |
| 176 | # reset the reference, to avoid circular reference |
| 177 | self._on_session_close = None |
| 178 | self._on_task_command = None |
| 179 | |
| 180 | cls = type(self) |
| 181 | if not nonblock: |
| 182 | self.unhandled_task_msgs.wait_empty(8) |
| 183 | |
| 184 | if not self.unhandled_task_msgs.empty(): |
| 185 | msg = self.unhandled_task_msgs.get() |
| 186 | logger.warning("%d unhandled task messages when session close. [%s]", len(msg), threading.current_thread()) |
| 187 | |
| 188 | for t in self.threads: |
| 189 | # delete registered thread |
| 190 | # so the `get_current_session()` call in those thread will raise SessionNotFoundException |
| 191 | del cls.thread2session[id(t)] |
| 192 | |
| 193 | if self.callback_thread: |
| 194 | del cls.thread2session[id(self.callback_thread)] |
| 195 | |
| 196 | def try_best_to_add_item_to_mq(mq, item, try_count=10): |
| 197 | for _ in range(try_count): |
| 198 | try: |
| 199 | mq.put(item, block=False) |
| 200 | return True |
| 201 | except queue.Full: |
| 202 | try: |
| 203 | mq.get(block=False) |
| 204 | except queue.Empty: |
| 205 | pass |
| 206 | |
| 207 | if self.callback_mq is not None: # 回调功能已经激活, 结束回调线程 |
| 208 | try_best_to_add_item_to_mq(self.callback_mq, None) |
| 209 | |
| 210 | for mq in self.task_mqs.values(): |
| 211 | try_best_to_add_item_to_mq(mq, None) # 消费端接收到None消息会抛出SessionClosedException异常 |
| 212 | self.task_mqs = {} |
| 213 | |
| 214 | def close(self, nonblock=False): |
| 215 | """关闭当前Session。由Backend调用""" |
no test coverage detected