在我的models.py中,我有一个类如何控制线程生命:
>>> s = [5, 4, 1, 4, 4, 4, 4, 4, 3]
>>> unique_array(s)
>>> s
[5, 4, 1, 3]
在repartidor.py中我声明了主题:
class Repartidor(models.Model):
thread_active = models.BooleanField(default=False)
def launch_thread(self):
self.thread_active = True
self.save()
def kill_thread(self):
self.thread_active = False
self.save()
def get_thread_state(self):
if self.thread_active:
return True
else:
return False
并在wsgi.py
中启动该主题class RepartidorThread(threading.Thread):
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, verbose=None):
super(RepartidorThread, self).__init__()
self.target = target
self.name = name
def run(self):
repartidor = Repartidor.objects.get(pk=1)
while True:
condition = repartidor.get_thread_state()
if condition:
do_something()
time.sleep(1)
else:
pass
time.sleep(1)
但我有问题,我在repartidor.py中用from printers.repartidor import RepartidorThread
thread_repartidor = RepartidorThread(name='Thread_Repartidor')
thread_repartidor.start()
读取条件并返回True或False。如果我通过django_admin或通过我的前端更改条件,则更新数据库的值,并且我的所有项目都读取新的valor,除了线程,它始终读取第一个。
如果我停止并启动服务器,该线程将读取新的valor。为什么会这样?