You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.3KB

  1. # -*- coding: utf-8 -*-
  2. from time import time
  3. from traceback import format_exc
  4. from loguru import logger
  5. from enums.ExceptionEnum import ExceptionType
  6. from exception.CustomerException import ServiceException
  7. def get_no_block_queue(queue):
  8. eBody = None
  9. try:
  10. eBody = queue.get(block=False)
  11. except Exception:
  12. pass
  13. return eBody
  14. def get_block_queue(queue):
  15. eBody = None
  16. try:
  17. eBody = queue.get()
  18. except Exception:
  19. pass
  20. return eBody
  21. def put_queue(queue, result, timeout=20, is_throw_ex=False):
  22. try:
  23. queue.put(result, timeout=timeout)
  24. except Exception:
  25. logger.error("添加队列异常:{}", format_exc())
  26. if is_throw_ex:
  27. raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
  28. ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
  29. def clear_queue(queue, is_ex=True):
  30. c_time = time()
  31. while True:
  32. if time() - c_time > 60:
  33. logger.error("清空队列失败, 情况队列超时!")
  34. if is_ex:
  35. raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
  36. ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
  37. break
  38. get_no_block_queue(queue)
  39. if queue.empty() or queue.qsize() == 0:
  40. break