def wrapper(*args, **kwargs): result = [] # create new args for _new_func, because we want to get the func return val to result list new_kwargs = { 'oldfunc': func, 'result': result, 'oldfunc_args': args, 'oldfunc_kwargs': kwargs } thd = KThread(target=_new_func, args=(), kwargs=new_kwargs) thd.start() thd.join(seconds) alive = thd.isAlive() thd.kill() # kill the child thread if alive: try: raise Timeout('request timeout') finally: return '' else: if result: return result[0] else: return ''