(*args, **kwargs)
| 23 | |
| 24 | @functools.wraps(func) |
| 25 | def wrapper(*args, **kwargs): |
| 26 | seconds = default_seconds |
| 27 | |
| 28 | if timeout_param: |
| 29 | # 先检查kwargs中是否有该参数 |
| 30 | if timeout_param in kwargs: |
| 31 | seconds = kwargs[timeout_param] |
| 32 | else: |
| 33 | # 如果kwargs中没有,尝试从位置参数中获取 |
| 34 | param_names = list(sig.parameters.keys()) |
| 35 | if timeout_param in param_names: |
| 36 | param_index = param_names.index(timeout_param) |
| 37 | if param_index < len(args): |
| 38 | seconds = args[param_index] |
| 39 | else: |
| 40 | # 如果位置参数中也没有,尝试从函数默认参数中获取 |
| 41 | param = sig.parameters[timeout_param] |
| 42 | if param.default != inspect.Parameter.empty: |
| 43 | seconds = param.default |
| 44 | |
| 45 | with ThreadPoolExecutor(max_workers=1) as executor: |
| 46 | future = executor.submit(func, *args, **kwargs) |
| 47 | try: |
| 48 | return future.result(timeout=seconds) |
| 49 | except concurrent.futures.TimeoutError: |
| 50 | raise TimeoutError(f"函数 {func.__name__} 执行超时({seconds}秒)") |
| 51 | |
| 52 | return wrapper |
| 53 | return decorator |
nothing calls this directly
no outgoing calls
no test coverage detected