12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- '''
- Created on 2019年8月1日
- @author: User
- '''
- import settings
- import pickle
- import time
- import json
- import numpy as np
- import ctypes
- import inspect
- #自定义jsonEncoder
- class MyEncoder(json.JSONEncoder):
- def default(self, obj):
- if isinstance(obj, np.ndarray):
- return obj.tolist()
- elif isinstance(obj, bytes):
- return str(obj, encoding='utf-8')
- elif isinstance(obj, (np.float_, np.float16, np.float32,
- np.float64)):
- return float(obj)
- return json.JSONEncoder.default(self, obj)
- TIMEOUT_RES = json.dumps({"code": "","name":"","prem":"","success":False})
- def run_timeout(db,id,step,jointime,sourceContent,doc_id):
- '''
- @summary: 设置超时处理
- '''
- if time.time()-jointime>settings.TIME_OUT:
-
- _timeout_data = dict()
- _timeout_data["id"] = id
- _timeout_data["step"] = step
- _timeout_data["data"] = sourceContent
- _timeout_data["doc_id"] = doc_id
- db.rpush(settings.TIMEOUT_QUEUE,json.dumps(_timeout_data,cls=MyEncoder))
- db.set(id,TIMEOUT_RES)
- return True
- return False
- def _async_raise(tid, exctype):
- """raises the exception, performs cleanup if needed"""
- tid = ctypes.c_long(tid)
- if not inspect.isclass(exctype):
- exctype = type(exctype)
- res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
- if res == 0:
- raise ValueError("invalid thread id")
- elif res != 1:
- ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
- raise SystemError("PyThreadState_SetAsyncExc failed")
- def stop_thread(thread):
- _async_raise(thread.ident, SystemExit)
- def getTimeOut(jointime):
- return settings.TIME_OUT-(time.time()-jointime)
|