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.

51 lines
1.5KB

  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. try:
  9. return queue.get(block=False)
  10. except Exception:
  11. return None
  12. def put_queue(queue, result, block=True, timeout=None, is_ex=False):
  13. try:
  14. queue.put(result, block=block, timeout=timeout)
  15. except Exception:
  16. logger.error("添加队列异常:{}", format_exc())
  17. if is_ex:
  18. raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
  19. ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
  20. def put_queue_result(queue, result, block=True, timeout=None):
  21. try:
  22. queue.put(result, block=block, timeout=timeout)
  23. return True
  24. except Exception:
  25. logger.error("添加队列异常:{}", format_exc())
  26. return False
  27. def clear_queue(queue):
  28. c_time = time()
  29. while True:
  30. if time() - c_time > 120:
  31. logger.error("清空队列失败, 情况队列超时!")
  32. raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
  33. ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
  34. get_no_block_queue(queue)
  35. if queue.empty() or queue.qsize() == 0:
  36. break
  37. # try:
  38. # queue.get_nowait()
  39. # except Exception:
  40. # break