You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.4 KiB

1 month ago
# encoding: utf-8
import requests, datetime, time
import threading
class TaskThread(threading.Thread):
def __init__(self, target, args=()):
super(TaskThread, self).__init__()
self.func = target
self.args = args
# 线程入口函数,名字不能变
def run(self):
self.result = self.func(self.args)
def GetResult(self):
try:
return self.result
except Exception:
return None
def LimitDecor():
def Functions(func):
def Run(params):
limit_time = params[0]
thre_func = TaskThread(target=func, args=params)
thre_func.setDaemon(True)
thre_func.start()
sleep_num = int(limit_time // 1)
sleep_left = round(limit_time % 1, 1)
while True:
time.sleep(1)
infor = thre_func.GetResult()
if infor:
return infor
sleep_num -= 1
if sleep_num <= 0:
break
time.sleep(sleep_left)
return thre_func.GetResult()
return Run
return Functions
def Start(limit_time, process):
theadiing = TaskThread(target=Task, args=[limit_time, process])
theadiing.start()
theadiing.join()
return theadiing.GetResult()
@LimitDecor()
def Task(args=()):
process = args[1]
return process.Run()