(self)
| 243 | logger.debug('Callback thread start') |
| 244 | |
| 245 | def _dispatch_callback_event(self): |
| 246 | while not self.closed(): |
| 247 | event = self.callback_mq.get() |
| 248 | if event is None: # 结束信号 |
| 249 | logger.debug('Callback thread exit') |
| 250 | break |
| 251 | |
| 252 | callback_info = self.callbacks.get(event['task_id']) |
| 253 | if not callback_info: |
| 254 | logger.error("No callback for callback_id:%s", event['task_id']) |
| 255 | return |
| 256 | callback, mutex = callback_info |
| 257 | |
| 258 | @wraps(callback) |
| 259 | def run(callback): |
| 260 | try: |
| 261 | callback(event['data']) |
| 262 | except Exception as e: |
| 263 | # 子类可能会重写 get_current_session ,所以不要用 ThreadBasedSession.get_current_session 来调用 |
| 264 | if not isinstance(e, SessionException): |
| 265 | self.on_task_exception() |
| 266 | |
| 267 | # todo: good to have -> clean up from `register_thread()` |
| 268 | |
| 269 | if mutex: |
| 270 | run(callback) |
| 271 | else: |
| 272 | t = threading.Thread(target=run, kwargs=dict(callback=callback), |
| 273 | daemon=True) |
| 274 | self.register_thread(t) |
| 275 | t.start() |
| 276 | |
| 277 | def register_callback(self, callback, serial_mode=False): |
| 278 | """ 向Session注册一个回调函数,返回回调id |
nothing calls this directly
no test coverage detected