Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

QueUtil.py 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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