README.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. | |downloads| |stars| |forks| |contributors| |coverage|
  2. | |version| |py-versions| |packages| |license|
  3. | |github-actions-wheels| |github-actions-bsd| |appveyor| |doc| |twitter| |tidelift|
  4. .. |downloads| image:: https://img.shields.io/pypi/dm/psutil.svg
  5. :target: https://pepy.tech/project/psutil
  6. :alt: Downloads
  7. .. |stars| image:: https://img.shields.io/github/stars/giampaolo/psutil.svg
  8. :target: https://github.com/giampaolo/psutil/stargazers
  9. :alt: Github stars
  10. .. |forks| image:: https://img.shields.io/github/forks/giampaolo/psutil.svg
  11. :target: https://github.com/giampaolo/psutil/network/members
  12. :alt: Github forks
  13. .. |contributors| image:: https://img.shields.io/github/contributors/giampaolo/psutil.svg
  14. :target: https://github.com/giampaolo/psutil/graphs/contributors
  15. :alt: Contributors
  16. .. |github-actions-wheels| image:: https://img.shields.io/github/actions/workflow/status/giampaolo/psutil/.github/workflows/build.yml.svg?label=Linux%2C%20macOS%2C%20Windows
  17. :target: https://github.com/giampaolo/psutil/actions?query=workflow%3Abuild
  18. :alt: Linux, macOS, Windows
  19. .. |github-actions-bsd| image:: https://img.shields.io/github/actions/workflow/status/giampaolo/psutil/.github/workflows/bsd.yml.svg?label=FreeBSD,%20NetBSD,%20OpenBSD
  20. :target: https://github.com/giampaolo/psutil/actions?query=workflow%3Absd-tests
  21. :alt: FreeBSD, NetBSD, OpenBSD
  22. .. |appveyor| image:: https://img.shields.io/appveyor/build/giampaolo/psutil/master.svg?maxAge=3600&label=Windows%20(py2)
  23. :target: https://ci.appveyor.com/project/giampaolo/psutil
  24. :alt: Windows (Appveyor)
  25. .. |coverage| image:: https://coveralls.io/repos/github/giampaolo/psutil/badge.svg?branch=master
  26. :target: https://coveralls.io/github/giampaolo/psutil?branch=master
  27. :alt: Test coverage (coverall.io)
  28. .. |doc| image:: https://readthedocs.org/projects/psutil/badge/?version=latest
  29. :target: https://psutil.readthedocs.io/en/latest/
  30. :alt: Documentation Status
  31. .. |version| image:: https://img.shields.io/pypi/v/psutil.svg?label=pypi
  32. :target: https://pypi.org/project/psutil
  33. :alt: Latest version
  34. .. |py-versions| image:: https://img.shields.io/pypi/pyversions/psutil.svg
  35. :alt: Supported Python versions
  36. .. |packages| image:: https://repology.org/badge/tiny-repos/python:psutil.svg
  37. :target: https://repology.org/metapackage/python:psutil/versions
  38. :alt: Binary packages
  39. .. |license| image:: https://img.shields.io/pypi/l/psutil.svg
  40. :target: https://github.com/giampaolo/psutil/blob/master/LICENSE
  41. :alt: License
  42. .. |twitter| image:: https://img.shields.io/twitter/follow/grodola.svg?label=follow&style=flat&logo=twitter&logoColor=4FADFF
  43. :target: https://twitter.com/grodola
  44. :alt: Twitter Follow
  45. .. |tidelift| image:: https://tidelift.com/badges/github/giampaolo/psutil?style=flat
  46. :target: https://tidelift.com/subscription/pkg/pypi-psutil?utm_source=pypi-psutil&utm_medium=referral&utm_campaign=readme
  47. :alt: Tidelift
  48. -----
  49. .. raw:: html
  50. <div align="center">
  51. <a href="https://github.com/giampaolo/psutil"><img src="https://github.com/giampaolo/psutil/raw/master/docs/_static/psutil-logo.png" /></a>
  52. <br />
  53. <br />
  54. <a href="https://github.com/giampaolo/psutil"><b>Home</b></a>&nbsp;&nbsp;&nbsp;
  55. <a href="https://github.com/giampaolo/psutil/blob/master/INSTALL.rst"><b>Install</b></a>&nbsp;&nbsp;&nbsp;
  56. <a href="https://psutil.readthedocs.io/"><b>Documentation</b></a>&nbsp;&nbsp;&nbsp;
  57. <a href="https://pypi.org/project/psutil/#files"><b>Download</b></a>&nbsp;&nbsp;&nbsp;
  58. <a href="https://groups.google.com/g/psutil"><b>Forum</b></a>&nbsp;&nbsp;&nbsp;
  59. <a href="https://gmpy.dev/tags/psutil"><b>Blog</b></a>&nbsp;&nbsp;&nbsp;
  60. <a href="#funding"><b>Funding</b></a>&nbsp;&nbsp;&nbsp;
  61. <a href="https://github.com/giampaolo/psutil/blob/master/HISTORY.rst"><b>What's new</b></a>&nbsp;&nbsp;&nbsp;
  62. </div>
  63. Summary
  64. =======
  65. psutil (process and system utilities) is a cross-platform library for
  66. retrieving information on **running processes** and **system utilization**
  67. (CPU, memory, disks, network, sensors) in Python.
  68. It is useful mainly for **system monitoring**, **profiling and limiting process
  69. resources** and **management of running processes**.
  70. It implements many functionalities offered by classic UNIX command line tools
  71. such as *ps, top, iotop, lsof, netstat, ifconfig, free* and others.
  72. psutil currently supports the following platforms:
  73. - **Linux**
  74. - **Windows**
  75. - **macOS**
  76. - **FreeBSD, OpenBSD**, **NetBSD**
  77. - **Sun Solaris**
  78. - **AIX**
  79. Supported Python versions are **2.7**, **3.6+** and
  80. `PyPy <http://pypy.org/>`__.
  81. Funding
  82. =======
  83. While psutil is free software and will always be, the project would benefit
  84. immensely from some funding.
  85. Keeping up with bug reports and maintenance has become hardly sustainable for
  86. me alone in terms of time.
  87. If you're a company that's making significant use of psutil you can consider
  88. becoming a sponsor via `GitHub Sponsors <https://github.com/sponsors/giampaolo>`__,
  89. `Open Collective <https://opencollective.com/psutil>`__ or
  90. `PayPal <https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=A9ZS7PKKRM3S8>`__
  91. and have your logo displayed in here and psutil `doc <https://psutil.readthedocs.io>`__.
  92. Sponsors
  93. ========
  94. .. raw:: html
  95. <div>
  96. <a href="https://tidelift.com/subscription/pkg/pypi-psutil?utm_source=pypi-psutil&utm_medium=referral&utm_campaign=readme">
  97. <img width="185" src="https://github.com/giampaolo/psutil/raw/master/docs/_static/tidelift-logo.svg" />
  98. </a>
  99. &nbsp;&nbsp
  100. <a href="https://sansec.io/">
  101. <img src="https://sansec.io/assets/images/logo.svg" />
  102. </a>
  103. </div>
  104. <sup><a href="https://github.com/sponsors/giampaolo">add your logo</a></sup>
  105. Supporters
  106. ==========
  107. .. raw:: html
  108. <div>
  109. <a href="https://github.com/dbwiddis"><img height="40" width="40" title="Daniel Widdis" src="https://avatars1.githubusercontent.com/u/9291703?s=88&amp;v=4" /></a>
  110. <a href="https://github.com/aristocratos"><img height="40" width="40" title="aristocratos" src="https://avatars3.githubusercontent.com/u/59659483?s=96&amp;v=4" /></a>
  111. <a href="https://github.com/cybersecgeek"><img height="40" width="40" title="cybersecgeek" src="https://avatars.githubusercontent.com/u/12847926?v=4" /></a>
  112. <a href="https://github.com/scoutapm-sponsorships"><img height="40" width="40" title="scoutapm-sponsorships" src="https://avatars.githubusercontent.com/u/71095532?v=4" /></a>
  113. <a href="https://opencollective.com/chenyoo-hao"><img height="40" width="40" title="Chenyoo Hao" src="https://images.opencollective.com/chenyoo-hao/avatar/40.png" /></a>
  114. <a href="https://opencollective.com/alexey-vazhnov"><img height="40" width="40" title="Alexey Vazhnov" src="https://images.opencollective.com/alexey-vazhnov/daed334/avatar/40.png" /></a>
  115. <a href="https://github.com/indeedeng"><img height="40" width="40" title="indeedeng" src="https://avatars.githubusercontent.com/u/2905043?s=200&v=4" /></a>
  116. <a href="https://github.com/PySimpleGUI"><img height="40" width="40" title="PySimpleGUI" src="https://avatars.githubusercontent.com/u/46163555?v=4" /></a>
  117. <a href="https://github.com/u93"><img height="40" width="40" title="Eugenio E Breijo" src="https://avatars.githubusercontent.com/u/16807302?v=4" /></a>
  118. <a href="https://github.com/guilt"><img height="40" width="40" title="Karthik Kumar Viswanathan" src="https://avatars.githubusercontent.com/u/195178?v=4" /></a>
  119. <a href="https://github.com/eallrich"><img height="40" width="40" title="Evan Allrich" src="https://avatars.githubusercontent.com/u/17393?v=4" /></a>
  120. <a href="https://github.com/robusta-dev"><img height="40" width="40" title="Robusta" src="https://avatars.githubusercontent.com/u/82757710?s=200&v=4" /></a>
  121. <a href="https://github.com/JeremyGrosser"><img height="40" width="40" title="JeremyGrosser" src="https://avatars.githubusercontent.com/u/2151?v=4" /></a>
  122. <a href="https://github.com/getsentry"><img height="40" width="40" title="getsentry" src="https://avatars.githubusercontent.com/u/1396951?s=200&v=4" /></a>
  123. <a href="https://github.com/c0m4r"><img height="40" width="40" title="c0m4r" src="https://avatars.githubusercontent.com/u/6292788?v=4" /></a>
  124. </div>
  125. <sup><a href="https://github.com/sponsors/giampaolo">add your avatar</a></sup>
  126. Contributing
  127. ============
  128. See `contributing guidelines <https://github.com/giampaolo/psutil/blob/master/CONTRIBUTING.md>`__.
  129. Example usages
  130. ==============
  131. This represents pretty much the whole psutil API.
  132. CPU
  133. ---
  134. .. code-block:: python
  135. >>> import psutil
  136. >>>
  137. >>> psutil.cpu_times()
  138. scputimes(user=3961.46, nice=169.729, system=2150.659, idle=16900.540, iowait=629.59, irq=0.0, softirq=19.42, steal=0.0, guest=0, guest_nice=0.0)
  139. >>>
  140. >>> for x in range(3):
  141. ... psutil.cpu_percent(interval=1)
  142. ...
  143. 4.0
  144. 5.9
  145. 3.8
  146. >>>
  147. >>> for x in range(3):
  148. ... psutil.cpu_percent(interval=1, percpu=True)
  149. ...
  150. [4.0, 6.9, 3.7, 9.2]
  151. [7.0, 8.5, 2.4, 2.1]
  152. [1.2, 9.0, 9.9, 7.2]
  153. >>>
  154. >>> for x in range(3):
  155. ... psutil.cpu_times_percent(interval=1, percpu=False)
  156. ...
  157. scputimes(user=1.5, nice=0.0, system=0.5, idle=96.5, iowait=1.5, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
  158. scputimes(user=1.0, nice=0.0, system=0.0, idle=99.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
  159. scputimes(user=2.0, nice=0.0, system=0.0, idle=98.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
  160. >>>
  161. >>> psutil.cpu_count()
  162. 4
  163. >>> psutil.cpu_count(logical=False)
  164. 2
  165. >>>
  166. >>> psutil.cpu_stats()
  167. scpustats(ctx_switches=20455687, interrupts=6598984, soft_interrupts=2134212, syscalls=0)
  168. >>>
  169. >>> psutil.cpu_freq()
  170. scpufreq(current=931.42925, min=800.0, max=3500.0)
  171. >>>
  172. >>> psutil.getloadavg() # also on Windows (emulated)
  173. (3.14, 3.89, 4.67)
  174. Memory
  175. ------
  176. .. code-block:: python
  177. >>> psutil.virtual_memory()
  178. svmem(total=10367352832, available=6472179712, percent=37.6, used=8186245120, free=2181107712, active=4748992512, inactive=2758115328, buffers=790724608, cached=3500347392, shared=787554304)
  179. >>> psutil.swap_memory()
  180. sswap(total=2097147904, used=296128512, free=1801019392, percent=14.1, sin=304193536, sout=677842944)
  181. >>>
  182. Disks
  183. -----
  184. .. code-block:: python
  185. >>> psutil.disk_partitions()
  186. [sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,nosuid'),
  187. sdiskpart(device='/dev/sda2', mountpoint='/home', fstype='ext', opts='rw')]
  188. >>>
  189. >>> psutil.disk_usage('/')
  190. sdiskusage(total=21378641920, used=4809781248, free=15482871808, percent=22.5)
  191. >>>
  192. >>> psutil.disk_io_counters(perdisk=False)
  193. sdiskio(read_count=719566, write_count=1082197, read_bytes=18626220032, write_bytes=24081764352, read_time=5023392, write_time=63199568, read_merged_count=619166, write_merged_count=812396, busy_time=4523412)
  194. >>>
  195. Network
  196. -------
  197. .. code-block:: python
  198. >>> psutil.net_io_counters(pernic=True)
  199. {'eth0': netio(bytes_sent=485291293, bytes_recv=6004858642, packets_sent=3251564, packets_recv=4787798, errin=0, errout=0, dropin=0, dropout=0),
  200. 'lo': netio(bytes_sent=2838627, bytes_recv=2838627, packets_sent=30567, packets_recv=30567, errin=0, errout=0, dropin=0, dropout=0)}
  201. >>>
  202. >>> psutil.net_connections(kind='tcp')
  203. [sconn(fd=115, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=48776), raddr=addr(ip='93.186.135.91', port=80), status='ESTABLISHED', pid=1254),
  204. sconn(fd=117, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=43761), raddr=addr(ip='72.14.234.100', port=80), status='CLOSING', pid=2987),
  205. ...]
  206. >>>
  207. >>> psutil.net_if_addrs()
  208. {'lo': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast='127.0.0.1', ptp=None),
  209. snicaddr(family=<AddressFamily.AF_INET6: 10>, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None, ptp=None),
  210. snicaddr(family=<AddressFamily.AF_LINK: 17>, address='00:00:00:00:00:00', netmask=None, broadcast='00:00:00:00:00:00', ptp=None)],
  211. 'wlan0': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='192.168.1.3', netmask='255.255.255.0', broadcast='192.168.1.255', ptp=None),
  212. snicaddr(family=<AddressFamily.AF_INET6: 10>, address='fe80::c685:8ff:fe45:641%wlan0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None),
  213. snicaddr(family=<AddressFamily.AF_LINK: 17>, address='c4:85:08:45:06:41', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]}
  214. >>>
  215. >>> psutil.net_if_stats()
  216. {'lo': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=65536, flags='up,loopback,running'),
  217. 'wlan0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=100, mtu=1500, flags='up,broadcast,running,multicast')}
  218. >>>
  219. Sensors
  220. -------
  221. .. code-block:: python
  222. >>> import psutil
  223. >>> psutil.sensors_temperatures()
  224. {'acpitz': [shwtemp(label='', current=47.0, high=103.0, critical=103.0)],
  225. 'asus': [shwtemp(label='', current=47.0, high=None, critical=None)],
  226. 'coretemp': [shwtemp(label='Physical id 0', current=52.0, high=100.0, critical=100.0),
  227. shwtemp(label='Core 0', current=45.0, high=100.0, critical=100.0)]}
  228. >>>
  229. >>> psutil.sensors_fans()
  230. {'asus': [sfan(label='cpu_fan', current=3200)]}
  231. >>>
  232. >>> psutil.sensors_battery()
  233. sbattery(percent=93, secsleft=16628, power_plugged=False)
  234. >>>
  235. Other system info
  236. -----------------
  237. .. code-block:: python
  238. >>> import psutil
  239. >>> psutil.users()
  240. [suser(name='giampaolo', terminal='pts/2', host='localhost', started=1340737536.0, pid=1352),
  241. suser(name='giampaolo', terminal='pts/3', host='localhost', started=1340737792.0, pid=1788)]
  242. >>>
  243. >>> psutil.boot_time()
  244. 1365519115.0
  245. >>>
  246. Process management
  247. ------------------
  248. .. code-block:: python
  249. >>> import psutil
  250. >>> psutil.pids()
  251. [1, 2, 3, 4, 5, 6, 7, 46, 48, 50, 51, 178, 182, 222, 223, 224, 268, 1215,
  252. 1216, 1220, 1221, 1243, 1244, 1301, 1601, 2237, 2355, 2637, 2774, 3932,
  253. 4176, 4177, 4185, 4187, 4189, 4225, 4243, 4245, 4263, 4282, 4306, 4311,
  254. 4312, 4313, 4314, 4337, 4339, 4357, 4358, 4363, 4383, 4395, 4408, 4433,
  255. 4443, 4445, 4446, 5167, 5234, 5235, 5252, 5318, 5424, 5644, 6987, 7054,
  256. 7055, 7071]
  257. >>>
  258. >>> p = psutil.Process(7055)
  259. >>> p
  260. psutil.Process(pid=7055, name='python3', status='running', started='09:04:44')
  261. >>> p.pid
  262. 7055
  263. >>> p.name()
  264. 'python3'
  265. >>> p.exe()
  266. '/usr/bin/python3'
  267. >>> p.cwd()
  268. '/home/giampaolo'
  269. >>> p.cmdline()
  270. ['/usr/bin/python3', 'main.py']
  271. >>>
  272. >>> p.ppid()
  273. 7054
  274. >>> p.parent()
  275. psutil.Process(pid=4699, name='bash', status='sleeping', started='09:06:44')
  276. >>> p.parents()
  277. [psutil.Process(pid=4699, name='bash', started='09:06:44'),
  278. psutil.Process(pid=4689, name='gnome-terminal-server', status='sleeping', started='0:06:44'),
  279. psutil.Process(pid=1, name='systemd', status='sleeping', started='05:56:55')]
  280. >>> p.children(recursive=True)
  281. [psutil.Process(pid=29835, name='python3', status='sleeping', started='11:45:38'),
  282. psutil.Process(pid=29836, name='python3', status='waking', started='11:43:39')]
  283. >>>
  284. >>> p.status()
  285. 'running'
  286. >>> p.create_time()
  287. 1267551141.5019531
  288. >>> p.terminal()
  289. '/dev/pts/0'
  290. >>>
  291. >>> p.username()
  292. 'giampaolo'
  293. >>> p.uids()
  294. puids(real=1000, effective=1000, saved=1000)
  295. >>> p.gids()
  296. pgids(real=1000, effective=1000, saved=1000)
  297. >>>
  298. >>> p.cpu_times()
  299. pcputimes(user=1.02, system=0.31, children_user=0.32, children_system=0.1, iowait=0.0)
  300. >>> p.cpu_percent(interval=1.0)
  301. 12.1
  302. >>> p.cpu_affinity()
  303. [0, 1, 2, 3]
  304. >>> p.cpu_affinity([0, 1]) # set
  305. >>> p.cpu_num()
  306. 1
  307. >>>
  308. >>> p.memory_info()
  309. pmem(rss=10915840, vms=67608576, shared=3313664, text=2310144, lib=0, data=7262208, dirty=0)
  310. >>> p.memory_full_info() # "real" USS memory usage (Linux, macOS, Win only)
  311. pfullmem(rss=10199040, vms=52133888, shared=3887104, text=2867200, lib=0, data=5967872, dirty=0, uss=6545408, pss=6872064, swap=0)
  312. >>> p.memory_percent()
  313. 0.7823
  314. >>> p.memory_maps()
  315. [pmmap_grouped(path='/lib/x8664-linux-gnu/libutil-2.15.so', rss=32768, size=2125824, pss=32768, shared_clean=0, shared_dirty=0, private_clean=20480, private_dirty=12288, referenced=32768, anonymous=12288, swap=0),
  316. pmmap_grouped(path='/lib/x8664-linux-gnu/libc-2.15.so', rss=3821568, size=3842048, pss=3821568, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=3821568, referenced=3575808, anonymous=3821568, swap=0),
  317. pmmap_grouped(path='[heap]', rss=32768, size=139264, pss=32768, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=32768, referenced=32768, anonymous=32768, swap=0),
  318. pmmap_grouped(path='[stack]', rss=2465792, size=2494464, pss=2465792, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=2465792, referenced=2277376, anonymous=2465792, swap=0),
  319. ...]
  320. >>>
  321. >>> p.io_counters()
  322. pio(read_count=478001, write_count=59371, read_bytes=700416, write_bytes=69632, read_chars=456232, write_chars=517543)
  323. >>>
  324. >>> p.open_files()
  325. [popenfile(path='/home/giampaolo/monit.py', fd=3, position=0, mode='r', flags=32768),
  326. popenfile(path='/var/log/monit.log', fd=4, position=235542, mode='a', flags=33793)]
  327. >>>
  328. >>> p.net_connections(kind='tcp')
  329. [pconn(fd=115, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=48776), raddr=addr(ip='93.186.135.91', port=80), status='ESTABLISHED'),
  330. pconn(fd=117, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=43761), raddr=addr(ip='72.14.234.100', port=80), status='CLOSING')]
  331. >>>
  332. >>> p.threads()
  333. [pthread(id=5234, user_time=22.5, system_time=9.2891),
  334. pthread(id=5237, user_time=0.0707, system_time=1.1)]
  335. >>>
  336. >>> p.num_threads()
  337. 4
  338. >>> p.num_fds()
  339. 8
  340. >>> p.num_ctx_switches()
  341. pctxsw(voluntary=78, involuntary=19)
  342. >>>
  343. >>> p.nice()
  344. 0
  345. >>> p.nice(10) # set
  346. >>>
  347. >>> p.ionice(psutil.IOPRIO_CLASS_IDLE) # IO priority (Win and Linux only)
  348. >>> p.ionice()
  349. pionice(ioclass=<IOPriority.IOPRIO_CLASS_IDLE: 3>, value=0)
  350. >>>
  351. >>> p.rlimit(psutil.RLIMIT_NOFILE, (5, 5)) # set resource limits (Linux only)
  352. >>> p.rlimit(psutil.RLIMIT_NOFILE)
  353. (5, 5)
  354. >>>
  355. >>> p.environ()
  356. {'LC_PAPER': 'it_IT.UTF-8', 'SHELL': '/bin/bash', 'GREP_OPTIONS': '--color=auto',
  357. 'XDG_CONFIG_DIRS': '/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg',
  358. ...}
  359. >>>
  360. >>> p.as_dict()
  361. {'status': 'running', 'num_ctx_switches': pctxsw(voluntary=63, involuntary=1), 'pid': 5457, ...}
  362. >>> p.is_running()
  363. True
  364. >>> p.suspend()
  365. >>> p.resume()
  366. >>>
  367. >>> p.terminate()
  368. >>> p.kill()
  369. >>> p.wait(timeout=3)
  370. <Exitcode.EX_OK: 0>
  371. >>>
  372. >>> psutil.test()
  373. USER PID %CPU %MEM VSZ RSS TTY START TIME COMMAND
  374. root 1 0.0 0.0 24584 2240 Jun17 00:00 init
  375. root 2 0.0 0.0 0 0 Jun17 00:00 kthreadd
  376. ...
  377. giampaolo 31475 0.0 0.0 20760 3024 /dev/pts/0 Jun19 00:00 python2.4
  378. giampaolo 31721 0.0 2.2 773060 181896 00:04 10:30 chrome
  379. root 31763 0.0 0.0 0 0 00:05 00:00 kworker/0:1
  380. >>>
  381. Further process APIs
  382. --------------------
  383. .. code-block:: python
  384. >>> import psutil
  385. >>> for proc in psutil.process_iter(['pid', 'name']):
  386. ... print(proc.info)
  387. ...
  388. {'pid': 1, 'name': 'systemd'}
  389. {'pid': 2, 'name': 'kthreadd'}
  390. {'pid': 3, 'name': 'ksoftirqd/0'}
  391. ...
  392. >>>
  393. >>> psutil.pid_exists(3)
  394. True
  395. >>>
  396. >>> def on_terminate(proc):
  397. ... print("process {} terminated".format(proc))
  398. ...
  399. >>> # waits for multiple processes to terminate
  400. >>> gone, alive = psutil.wait_procs(procs_list, timeout=3, callback=on_terminate)
  401. >>>
  402. Windows services
  403. ----------------
  404. .. code-block:: python
  405. >>> list(psutil.win_service_iter())
  406. [<WindowsService(name='AeLookupSvc', display_name='Application Experience') at 38850096>,
  407. <WindowsService(name='ALG', display_name='Application Layer Gateway Service') at 38850128>,
  408. <WindowsService(name='APNMCP', display_name='Ask Update Service') at 38850160>,
  409. <WindowsService(name='AppIDSvc', display_name='Application Identity') at 38850192>,
  410. ...]
  411. >>> s = psutil.win_service_get('alg')
  412. >>> s.as_dict()
  413. {'binpath': 'C:\\Windows\\System32\\alg.exe',
  414. 'description': 'Provides support for 3rd party protocol plug-ins for Internet Connection Sharing',
  415. 'display_name': 'Application Layer Gateway Service',
  416. 'name': 'alg',
  417. 'pid': None,
  418. 'start_type': 'manual',
  419. 'status': 'stopped',
  420. 'username': 'NT AUTHORITY\\LocalService'}
  421. Projects using psutil
  422. =====================
  423. Here's some I find particularly interesting:
  424. - https://github.com/google/grr
  425. - https://github.com/facebook/osquery/
  426. - https://github.com/nicolargo/glances
  427. - https://github.com/aristocratos/bpytop
  428. - https://github.com/Jahaja/psdash
  429. - https://github.com/ajenti/ajenti
  430. - https://github.com/home-assistant/home-assistant/
  431. Portings
  432. ========
  433. - Go: https://github.com/shirou/gopsutil
  434. - C: https://github.com/hamon-in/cpslib
  435. - Rust: https://github.com/rust-psutil/rust-psutil
  436. - Nim: https://github.com/johnscillieri/psutil-nim