首先,我是一个蟒蛇新手,我希望你可以帮我解决我的问题。 我的程序(我正在使用tkinter )假设检查进程是否正在运行,如果它没有运行,我想自己运行它。 我想运行每N秒检查一次进程的函数,我也希望我的应用程序在执行这个函数时不要'冻结',所以我决定使用线程,我创建了一个新的类来创建我的线程并运行我的每个时间间隔的功能。
class TaskThread(threading.Thread):
"""Thread that executes a task every N seconds"""
def __init__(self, interval, callback):
threading.Thread.__init__(self)
self._finished = threading.Event()
self.setInterval(interval)
self.callback = callback
self.run()
def setInterval(self, interval):
"""Set the number of seconds we sleep between executing our task"""
self._interval = interval
def shutdown(self):
"""Stop this thread"""
self._finished.set()
def run(self):
while 1:
if self._finished.isSet(): return
self.callback()
# sleep for interval or until shutdown
self._finished.wait(self._interval)
def process_running(self):
"check if the process is running"
print("test")
def check_process():
"check if process is alive"
for process in psutil.process_iter():
if self.process_name == process.name():
self.process_id = process.pid
return True
return False
if not check_process():
self.process = subprocess.Popen([self.launcher_exe])
time.sleep(20.0) #let the notepad.exe to load completely
self.process_id = self.process.pid
self.hwnd = self.get_hwnds_for_pid(self.process_id) #get the window handle
self.loop = TaskThread(10, self.process_running)
现在,当我启动我的应用程序时,程序正在运行check_process函数,但应用程序窗口没有响应,我根本无法使用该窗口。
我想知道我做错了什么,希望你能帮助我。