Python - 这个线程/循环/等待代码不好吗 - 它 'pegs' cpu
Python - Is this threading / looping / waiting code bad - It 'pegs' cpu
我有一个 python 应用程序,其中线程 1 调用 api 来查看 'what reports are ready to download' 并将该 report_id 发送到线程 2,其中“downloads/processes 那些报告。这些线程遍历字典然后等待 5 分钟。即使代码是 'doing nothing',它也会挂钩 CPU。
我不确定 CPU 是在线程中还是在主线程中。主要是我有一些处理程序来检查停止信号,所以我发布了大部分代码。我有几个线程以类似的方式执行类似的任务,它们在循环结束时等待。我怀疑代码与 cpu 的挂钩相关的区域 a) 在主要部分 - while run: pass
。 b) 在每个线程中 is_killed = self._kill.wait(600)
并且是 theading.Event()
知道什么是挂钩 cpu。
if __name__ == '__main__':
t2 = ProcessReport()
t2.start()
t1 = RequestReport(t2)
t1.start()
t3 = report_test()
t3.start()
t4 = run_flask()
t4.start()
run = True
signal.signal(signal.SIGINT, handler_stop_signals)
signal.signal(signal.SIGTERM, handler_stop_signals)
signal.signal(signal.SIGHUP, handler_stop_signals)
while run:
pass # Stay here until kill
print("About to kill all threads in clean order")
t3.kill()
t3.join()
t1.kill()
t1.join()
t2.kill()
t2.join()
print("Clean Exit")
sys.exit()
信号处理器
def handler_stop_signals(signum, frame):
global run
run = False
线程之一
class report_test(Thread):
def __init__(self):
Thread.__init__(self)
self._kill = threading.Event()
def kill(self):
self._kill.set()
def run(self):
while True:
cursor.execute("SELECT * FROM tbl_rpt_log where cron='1'")
reports_to_run = cursor.fetchall()
for row in reports_to_run:
report_on_row(row)
is_killed = self._kill.wait(600)
if is_killed:
print("Killing - ReportCheckTable")
break
问题(至少是主要问题)是:
while run:
pass # Stay here until kill
这是因为这里唯一的操作是评估循环的条件和 CPU“无法中断”。
我没有通读整个代码来理解它的高层次,但解决它的最快方法是:
import time
# ...
while run:
time.sleep(0.1) # Stay here until kill
我有一个 python 应用程序,其中线程 1 调用 api 来查看 'what reports are ready to download' 并将该 report_id 发送到线程 2,其中“downloads/processes 那些报告。这些线程遍历字典然后等待 5 分钟。即使代码是 'doing nothing',它也会挂钩 CPU。
我不确定 CPU 是在线程中还是在主线程中。主要是我有一些处理程序来检查停止信号,所以我发布了大部分代码。我有几个线程以类似的方式执行类似的任务,它们在循环结束时等待。我怀疑代码与 cpu 的挂钩相关的区域 a) 在主要部分 - while run: pass
。 b) 在每个线程中 is_killed = self._kill.wait(600)
并且是 theading.Event()
知道什么是挂钩 cpu。
if __name__ == '__main__':
t2 = ProcessReport()
t2.start()
t1 = RequestReport(t2)
t1.start()
t3 = report_test()
t3.start()
t4 = run_flask()
t4.start()
run = True
signal.signal(signal.SIGINT, handler_stop_signals)
signal.signal(signal.SIGTERM, handler_stop_signals)
signal.signal(signal.SIGHUP, handler_stop_signals)
while run:
pass # Stay here until kill
print("About to kill all threads in clean order")
t3.kill()
t3.join()
t1.kill()
t1.join()
t2.kill()
t2.join()
print("Clean Exit")
sys.exit()
信号处理器
def handler_stop_signals(signum, frame):
global run
run = False
线程之一
class report_test(Thread):
def __init__(self):
Thread.__init__(self)
self._kill = threading.Event()
def kill(self):
self._kill.set()
def run(self):
while True:
cursor.execute("SELECT * FROM tbl_rpt_log where cron='1'")
reports_to_run = cursor.fetchall()
for row in reports_to_run:
report_on_row(row)
is_killed = self._kill.wait(600)
if is_killed:
print("Killing - ReportCheckTable")
break
问题(至少是主要问题)是:
while run:
pass # Stay here until kill
这是因为这里唯一的操作是评估循环的条件和 CPU“无法中断”。
我没有通读整个代码来理解它的高层次,但解决它的最快方法是:
import time
# ...
while run:
time.sleep(0.1) # Stay here until kill