Skip to content

Commit

Permalink
add kill process tree
Browse files Browse the repository at this point in the history
  • Loading branch information
vertexi committed Feb 9, 2022
1 parent b376dc0 commit 11b46c1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- [x] close handle
- [x] file icon
- [x] improve file icon
- [ ] kill process tree
- [x] kill process tree
- [x] select search type
- [x] case sensitive
- [x] after kill fast refresh(status bar)
Expand Down
28 changes: 28 additions & 0 deletions src/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,31 @@ def stop_self_process(self):
if self.process.poll() is None:
self.process.kill()

def kill_proc_tree(self, target: MyTreeWidgetItem):
"""Kill a process tree (including grandchildren) with signal
"sig" and return a (gone, still_alive) tuple.
"on_terminate", if specified, is a callback function which is
called as soon as a child terminates.
"""
pid = int(target.datum)
assert pid != os.getpid(), "won't kill myself"
try:
parent = psutil.Process(pid)
except Exception as e:
self.send_kill_status_message.emit(f"failed: kill ({target.text(0)}) process failed.{e}")
return
children = parent.children(recursive=True)
children.append(parent)
for p in children:
try:
p.kill()
except:
pass
psutil.wait_procs(children, timeout=1)
if psutil.pid_exists(pid):
self.send_kill_status_message.emit(f"failed: kill ({target.text(0)}) process failed.")
return
else:
self.send_kill_status_message.emit(f"success: kill ({target.text(0)}) process.")
self.clean_killed_tree_item.emit(target)
return
10 changes: 10 additions & 0 deletions src/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class MyTreeWidget(QTreeWidget):
kill_process_signal = pyqtSignal(list)
kill_handle_signal: pyqtBoundSignal
kill_handle_signal = pyqtSignal(list)
kill_process_tree_signal: pyqtBoundSignal
kill_process_tree_signal = pyqtSignal(MyTreeWidgetItem)
send_status_message: pyqtBoundSignal
send_status_message = pyqtSignal(str)

Expand Down Expand Up @@ -132,6 +134,10 @@ def _show_context_menu(self, position):
bring_to_front.triggered.connect(self.bring_to_front)
menu.addAction(bring_to_front)

kill_process_tree = QAction("Kill process tree")
kill_process_tree.triggered.connect(self.kill_process_tree)
menu.addAction(kill_process_tree)

open_file_in_explorer = QAction("Reveal in Explorer")
open_file_in_explorer.triggered.connect(self.open_file_in_explorer)
menu.addAction(open_file_in_explorer)
Expand Down Expand Up @@ -170,6 +176,9 @@ def bring_to_front(self):
self.send_status_message.emit(f"pid:{pid} windows brint to front failed.{e}")
pass

def kill_process_tree(self):
self.kill_process_tree_signal.emit(self.currentItem())


class MyLineEdit(QLineEdit):
textChanged: pyqtBoundSignal
Expand Down Expand Up @@ -334,6 +343,7 @@ def __init__(self):
self.process_tree.kill_process_signal.connect(self.kill_task.kill_process)
self.process_tree.kill_handle_signal.connect(self.kill_task.kill_handle)
self.process_tree.send_status_message.connect(self.append_status_message)
self.process_tree.kill_process_tree_signal.connect(self.kill_task.kill_proc_tree)

self.refresh_pushbutton.clicked.connect(self.send_to_start_proc)
self.refresh_pushbutton.clicked.connect(self.process_tree.clear_me)
Expand Down

0 comments on commit 11b46c1

Please sign in to comment.