xkits-thread 0.2__tar.gz → 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xkits-thread
3
- Version: 0.2
3
+ Version: 0.2.1
4
4
  Summary: Thread module
5
5
  Home-page: https://github.com/bondbox/xthreads/
6
6
  Author: Mingzhe Zou
@@ -16,7 +16,8 @@ Classifier: Programming Language :: Python :: 3
16
16
  Requires-Python: >=3.8
17
17
  Description-Content-Type: text/markdown
18
18
  License-File: LICENSE
19
- Requires-Dist: xkits-lib>=0.1
19
+ Requires-Dist: xkits-lib>=0.4
20
+ Requires-Dist: psutil
20
21
 
21
22
  # xthreads
22
23
 
@@ -3,6 +3,7 @@
3
3
  from xkits_thread.executor import ThreadPool # noqa:F401
4
4
  from xkits_thread.executor import hourglass # noqa:F401,H306
5
5
  from xkits_thread.lock import NamedLock # noqa:F401
6
+ from xkits_thread.proc import Processes # noqa:F401
6
7
  from xkits_thread.task import DaemonTaskJob # noqa:F401
7
8
  from xkits_thread.task import DelayTaskJob # noqa:F401
8
9
  from xkits_thread.task import TaskJob # noqa:F401
@@ -1,7 +1,7 @@
1
1
  # coding:utf-8
2
2
 
3
3
  __project__ = "xkits-thread"
4
- __version__ = "0.2"
4
+ __version__ = "0.2.1"
5
5
  __urlhome__ = "https://github.com/bondbox/xthreads/"
6
6
  __description__ = "Thread module"
7
7
 
@@ -0,0 +1,68 @@
1
+ # coding:utf-8
2
+
3
+ from typing import Dict
4
+ from typing import Iterator
5
+ from typing import List
6
+ from typing import Union
7
+
8
+ from psutil import Process
9
+
10
+
11
+ class Processes():
12
+
13
+ def __init__(self) -> None:
14
+ self.__processes: Dict[int, Process] = {}
15
+
16
+ def __iter__(self) -> Iterator[Process]:
17
+ invalid: List[Process] = []
18
+
19
+ for pid in sorted(self.__processes.keys()):
20
+ if not (obj := self.__processes[pid]).is_running():
21
+ invalid.append(obj)
22
+ continue
23
+
24
+ yield obj
25
+
26
+ for obj in invalid:
27
+ del self.__processes[obj.pid] # pragma: no cover
28
+
29
+ def __getitem__(self, pid: int):
30
+ if pid not in self.__processes:
31
+ obj: Process = Process(pid=pid)
32
+ self.__processes.setdefault(pid, obj)
33
+ return self.__processes[pid]
34
+
35
+ def __len__(self) -> int:
36
+ return len(self.__processes)
37
+
38
+ def add(self, p: Union[Process, int]) -> bool:
39
+ obj: Process = Process(pid=p) if isinstance(p, int) else p
40
+ assert isinstance(obj, Process), f"{type(obj)} is not Process"
41
+
42
+ if (pid := obj.pid) in self.__processes:
43
+ if (proc := self.__processes[pid]).is_running():
44
+ return proc == obj
45
+ del self.__processes[pid]
46
+
47
+ self.__processes.setdefault(pid, obj)
48
+ return self.__processes[pid] is obj
49
+
50
+ def select(self, name: str, exact: bool = True) -> None:
51
+ def _name_filter(process_name: str) -> bool:
52
+ return process_name == name if exact else name in process_name
53
+
54
+ from psutil import process_iter # pylint:disable=C0415
55
+ for proc in process_iter(["name"]):
56
+ if _name_filter(proc.name()):
57
+ self.add(proc)
58
+
59
+ @classmethod
60
+ def search(cls, name: str, exact: bool = True) -> "Processes":
61
+ instance: Processes = cls()
62
+ instance.select(name, exact)
63
+ return instance
64
+
65
+
66
+ if __name__ == "__main__":
67
+ for _p in Processes.search("systemd"):
68
+ print(f"{_p.name()} pid:{_p.pid}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xkits-thread
3
- Version: 0.2
3
+ Version: 0.2.1
4
4
  Summary: Thread module
5
5
  Home-page: https://github.com/bondbox/xthreads/
6
6
  Author: Mingzhe Zou
@@ -16,7 +16,8 @@ Classifier: Programming Language :: Python :: 3
16
16
  Requires-Python: >=3.8
17
17
  Description-Content-Type: text/markdown
18
18
  License-File: LICENSE
19
- Requires-Dist: xkits-lib>=0.1
19
+ Requires-Dist: xkits-lib>=0.4
20
+ Requires-Dist: psutil
20
21
 
21
22
  # xthreads
22
23
 
@@ -6,6 +6,7 @@ xkits_thread/__init__.py
6
6
  xkits_thread/attribute.py
7
7
  xkits_thread/executor.py
8
8
  xkits_thread/lock.py
9
+ xkits_thread/proc.py
9
10
  xkits_thread/task.py
10
11
  xkits_thread.egg-info/PKG-INFO
11
12
  xkits_thread.egg-info/SOURCES.txt
@@ -0,0 +1,2 @@
1
+ xkits-lib>=0.4
2
+ psutil
@@ -1 +0,0 @@
1
- xkits-lib>=0.1
File without changes
File without changes
File without changes
File without changes