如何在python中杀死守护进程线程?

时间:2020-07-18 12:47:07

标签: python multithreading tkinter python-multithreading

我正在尝试使用python创建一个“ Alarm”程序,其中使用了线程。 要连续运行代码,我必须杀死启动的守护程序线程以发出警报声,但我无法这样做。 请指导我。这是我的代码:

import tkinter
from tkinter import *
import time
import winsound
import threading
import multiprocessing
import queue
global a
global m
global t
global z
global s
s=0
def ring(s,q):
    while True:
      winsound.PlaySound("D:/file_example_WAV_1MG.wav", winsound.SND_ASYNC|winsound.SND_LOOP|winsound.SND_FILENAME)
      s=q.get()
      print("s")
      if s==1:
          s=0
          q.put(s)
          winsound.PlaySound(None,winsound.SND_ASYNC | winsound.SND_LOOP | winsound.SND_FILENAME)
          break
class A:
    def __init__(self):
        self.top = tkinter.Tk(None, None, "Alarm")
        self.top.wm_geometry("300x130")
        text = Text(self.top)
        text.grid(row=0, column=1)
        B = tkinter.Button(self.top, text="ACK", command=self.close_top, fg='red', bd=3, font=('Centura', 10, 'normal'),
                           bg='yellow', width=4).place(x=100, y=100)
        text.insert(INSERT, "Load program track file")

    def close_top(self):
        print("closing")
        s=1
        q.put(s)
        time.sleep(0.2)
        self.top.destroy()
        time.sleep(1)
        t.join()
        print(t.is_alive())
        print(t)
        print("go to main")
    def main(self):
     while True:
        z = 0
        print(z)
        time.sleep(2)
        z = q.get()
        print(z)
        if z == 1:
            z = 0
            q.put(z)
            if not t.is_alive():
             print(t)
             t.start()
            new.exec()
            break
    def exec(self):
        print("exec")
        self.top.mainloop()
new = A()
def run():
    print("run")
    while True:
        time.sleep(1)
        if int(time.strftime("%S")) == 10:
            z=1
            q.put(z)
            break
q=queue.Queue()
t = threading.Thread(target=ring, args=(s,q))
t.daemon=True
while True:
    run()
    new.main()

运行代码后,出现错误“线程只能启动一次”

0 个答案:

没有答案