这是我的第一个线程程序.我在这里面临一个奇怪的问题.我正在Django中构建一个类似于应用程序的简单调度程序,其中函数名(将定期执行)及其下一次执行时间将存储在Django Model中.执行管理命令以启动线程,该线程连续运行以检查是否应执行任何功能,如果是,则启动新线程以执行该功能.这样,为每个函数创建了单独的线程(至少就是这个主意!).
class Command(BaseCommand):
def __init__(self):
super(Command, self).__init__()
self.lock = None
def handle(self, *args, **kwargs):
self.lock = threading.RLock()
t1 = threading.Thread(target=self.cron_thread)
t1.start()
t1.join()
def cron_thread(self):
while True:
# Fetch only Active records
scheduled_actions = Scheduler.objects.filter(active=True)
for scheduled_action in scheduled_actions:
# check if execution is due
if scheduled_action.next_execution_time == datetime.now():
# creating a new thread
function_thread = threading.Thread(target=eval(scheduled_action.function_name),