简体   繁体   English

无法停止运行线程

[英]Can't stop running thread

I've a python program executing 3 different threads.我有一个执行 3 个不同线程的 python 程序。

t1 = Thread(target=calculate_statistics, args=(ip_slave_1, path_script_slave_1, logger))
t1.daemon = True
t1.start() 
t2 = Thread(target=calculate_statistics, args=(ip_slave_2, path_script_slave_2, logger))
t2.daemon = True
t2.start() 
t3 = Thread(target=computing, args=("1"))
t3.daemon = True
t3.start() 
t1.join()
t2.join()
t3.join()

Thread are executing well.线程执行良好。 The problem is that to stop the whole program, I have to digit many times CTRL + C. I see this message on the screen: (FIRST CTRL + C) :问题是要停止整个程序,我必须多次按CTRL + C。我在屏幕上看到这条消息: (FIRST CTRL + C)

^CTraceback (most recent call last):
  File "/mnt/roaming.py", line 116, in <module>
    t1.join()
  File "/usr/lib/python3.9/threading.py", line 1060, in join
  File "/usr/lib/python3.9/threading.py", line 1080, in _wait_for_tstate_lock

c (SECOND CTRL + C) c (第二个 CTRL + C)

^CException ignored in: <module 'threading' from '/usr/lib/python3.9/threading.pyc'>
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 1477, in _shutdown
KeyboardInterrupt:

Each thread is executing an infinite loop.每个线程都在执行一个无限循环。 How can I kill the threads when I press CTRL + C ?CTRL + C时如何杀死线程? This are the threads functions:这是线程函数:

def calculate_statistics(ip_slave, path_script_slave, logger):
    while True:
      current_time = datetime.datetime.now()
      result = os.popen("perl " + str(path_script_slave) + "SYSTEMCMD.pl uf_rxinfo all").read()
      rssi = result.split(",",1)[1]
      rssi = rssi.split(",",1)[0]
      agc = result.split(",",1)[1]
      agc = agc.split(",",1)[1]
      agc = agc.split(",",1)[0]
      auth = result.split(",",1)[1]
      auth = auth.split(",",1)[1]
      auth = auth.split(",",1)[0]
      print("[" + str(current_time) + "]"+"[SLAVE]" + "[" + str(ip_slave) + "] " +"RSSI: " + str(rssi) + "; AGC: " + str(agc) + "; AUTH: " + str(auth) +".")
      row = str(current_time) + " -IP " + str(ip_slave) + " -RSSI " + str(rssi) + " -AGC " + str(agc) + " -AUTH " + str(auth)
      logger.info(row)
      time.sleep(0.2)
def computing(f_name):
    time.sleep(1)
    while True:
      a_file = open("slaves.log", "r")
      lines = a_file.readlines()
      last_lines = lines[-8:]
      rssi_slave_1 = []
      rssi_slave_2 = []
      agc_slave_1 = []
      agc_slave_2 = []
      auth_slave_1 = []
      auth_slave_2 = []
      f = open("diff.log", "w")
      for element in last_lines:
        f.write(str(element))
        if(element.split("-IP")[1].split(" ")[1]) == str(ip_slave_1):
          rssi_slave_1.append(element.split("-RSSI")[1].split(" ")[1])
          agc_slave_1.append(element.split("-AGC")[1].split(" ")[1])
          auth_slave_1.append(element.split("-AUTH")[1].split(" ")[1].split("\n")[0])
        else:
          rssi_slave_2.append(element.split("-RSSI")[1].split(" ")[1])
          agc_slave_2.append(element.split("-AGC")[1].split(" ")[1])
          auth_slave_2.append(element.split("-AUTH")[1].split(" ")[1].split("\n")[0])          
      f.close()
      print(rssi_slave_1)
      print(rssi_slave_2)
      print(agc_slave_1)
      print(agc_slave_2)
      print(auth_slave_1)
      print(auth_slave_2)
      algorithm(rssi_slave_1,rssi_slave_2,agc_slave_1,agc_slave_2,auth_slave_1,auth_slave_2)
      time.sleep(1)

The canonical way is to have your main thread wait in an infinite loop, and signal the other threads with a threading.Event and replace any infinite loop within the threads with something that waits for that event to have been set.规范的方法是让您的主线程在无限循环中等待,并用threading.Event向其他线程发出信号,并用等待该事件设置的东西替换线程内的任何无限循环。

stop_event.wait(x) would replace any time.sleep(x) you'd have in the processing loops. stop_event.wait(x)将替换您在处理循环中的任何 time.sleep(x time.sleep(x)

import random
import threading
import time

stop_event = threading.Event()


def ponder(thing):
    while not stop_event.wait(random.uniform(0.9, 1.1)):
        print(f"Thinking about {thing}...")
    print(f"Ah! Stopping thinking about {thing}.")


threads = [
    threading.Thread(target=ponder, args=(arg,))
    for arg in ["spam", "eggs", "toast"]
]

for thread in threads:
    thread.start()

while True:
    try:
        time.sleep(1)
    except KeyboardInterrupt:
        print("Keyboard interrupt caught.")
        stop_event.set()
        break

for thread in threads:
    thread.join()

print("Done!")

The output of this example is eg这个例子的输出是例如

Thinking about eggs...
Thinking about spam...
Thinking about toast...
Thinking about eggs...
Thinking about spam...
Thinking about toast...
Thinking about toast...
Thinking about eggs...
Thinking about spam...
Thinking about spam...
Thinking about toast...
Thinking about eggs...
^CKeyboard interrupt caught.
Ah! Stopping thinking about spam.
Ah! Stopping thinking about toast.
Ah! Stopping thinking about eggs.
Done!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM