|
2 | 2 | import multiprocessing
|
3 | 3 | import os
|
4 | 4 | import platform
|
| 5 | +import psutil |
5 | 6 | import random
|
6 | 7 | import subprocess
|
7 | 8 | import time
|
8 | 9 | import webbrowser
|
9 |
| -from typing import Union |
| 10 | +from typing import Union, List |
| 11 | + |
10 | 12 |
|
11 | 13 | import pyperclip
|
12 | 14 | from botcity.base import BaseBot, State
|
13 | 15 | from botcity.base.utils import is_retina, only_if_element
|
14 | 16 | from PIL import Image, ImageGrab
|
| 17 | +from psutil import Process |
15 | 18 | from pynput.keyboard import Controller as KbController
|
16 | 19 | from pynput.keyboard import Key
|
17 | 20 | from pynput.mouse import Controller as MouseController
|
@@ -443,6 +446,38 @@ def find_text(self, label, x=None, y=None, width=None, height=None, *, threshold
|
443 | 446 | return self.find_until(label, x, y, width, height, threshold=threshold, matching=matching,
|
444 | 447 | waiting_time=waiting_time, best=best, grayscale=True)
|
445 | 448 |
|
| 449 | + def find_process(self, name: str = None, pid: str = None) -> List[Process]: |
| 450 | + """ |
| 451 | + Find a process by name or PID |
| 452 | +
|
| 453 | + Args: |
| 454 | + name (str): The process name. |
| 455 | + pid (str) or (int): The PID (Process Identifier). |
| 456 | +
|
| 457 | + Return: |
| 458 | + process (psutil.Process): A Process instance. |
| 459 | + """ |
| 460 | + for process in psutil.process_iter(): |
| 461 | + try: |
| 462 | + if (name is not None and name in process.name()) or \ |
| 463 | + (pid is not None and process.pid == pid): |
| 464 | + return process |
| 465 | + except (psutil.NoSuchProcess, psutil.AccessDenied): |
| 466 | + pass |
| 467 | + return None |
| 468 | + |
| 469 | + def terminate_process(self, process: Process): |
| 470 | + """ |
| 471 | + Terminate the process via the received Process object. |
| 472 | +
|
| 473 | + Args: |
| 474 | + process (psutil.Process): The process to terminate. |
| 475 | + """ |
| 476 | + process.terminate() |
| 477 | + process.wait(10) |
| 478 | + if process.is_running(): |
| 479 | + raise Exception("Terminate process failed") |
| 480 | + |
446 | 481 | def get_last_element(self):
|
447 | 482 | """
|
448 | 483 | Return the last element found.
|
|
0 commit comments