run_utils.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. '''
  2. Created on 2019年8月1日
  3. @author: User
  4. '''
  5. import settings
  6. import pickle
  7. import time
  8. import json
  9. import numpy as np
  10. import ctypes
  11. import inspect
  12. #自定义jsonEncoder
  13. class MyEncoder(json.JSONEncoder):
  14. def default(self, obj):
  15. if isinstance(obj, np.ndarray):
  16. return obj.tolist()
  17. elif isinstance(obj, bytes):
  18. return str(obj, encoding='utf-8')
  19. elif isinstance(obj, (np.float_, np.float16, np.float32,
  20. np.float64)):
  21. return float(obj)
  22. return json.JSONEncoder.default(self, obj)
  23. TIMEOUT_RES = json.dumps({"code": "","name":"","prem":"","success":False})
  24. def run_timeout(db,id,step,jointime,sourceContent,doc_id):
  25. '''
  26. @summary: 设置超时处理
  27. '''
  28. if time.time()-jointime>settings.TIME_OUT:
  29. _timeout_data = dict()
  30. _timeout_data["id"] = id
  31. _timeout_data["step"] = step
  32. _timeout_data["data"] = sourceContent
  33. _timeout_data["doc_id"] = doc_id
  34. db.rpush(settings.TIMEOUT_QUEUE,json.dumps(_timeout_data,cls=MyEncoder))
  35. db.set(id,TIMEOUT_RES)
  36. return True
  37. return False
  38. def _async_raise(tid, exctype):
  39. """raises the exception, performs cleanup if needed"""
  40. tid = ctypes.c_long(tid)
  41. if not inspect.isclass(exctype):
  42. exctype = type(exctype)
  43. res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
  44. if res == 0:
  45. raise ValueError("invalid thread id")
  46. elif res != 1:
  47. ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
  48. raise SystemError("PyThreadState_SetAsyncExc failed")
  49. def stop_thread(thread):
  50. _async_raise(thread.ident, SystemExit)
  51. def getTimeOut(jointime):
  52. return settings.TIME_OUT-(time.time()-jointime)