|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- # -*- coding: utf-8 -*-
- from time import time
- from traceback import format_exc
-
- from loguru import logger
-
- from enums.ExceptionEnum import ExceptionType
- from exception.CustomerException import ServiceException
-
-
- def get_no_block_queue(queue):
- eBody = None
- try:
- eBody = queue.get(block=False)
- except Exception:
- pass
- return eBody
-
-
- def get_block_queue(queue):
- eBody = None
- try:
- eBody = queue.get()
- except Exception:
- pass
- return eBody
-
-
- def put_queue(queue, result, timeout=20, is_throw_ex=False):
- try:
- queue.put(result, timeout=timeout)
- except Exception:
- logger.error("添加队列异常:{}", format_exc())
- if is_throw_ex:
- raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
- ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
-
- def clear_queue(queue, is_ex=True):
- c_time = time()
- while True:
- if time() - c_time > 60:
- logger.error("清空队列失败, 情况队列超时!")
- if is_ex:
- raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
- ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
- break
- get_no_block_queue(queue)
- if queue.empty() or queue.qsize() == 0:
- break
|