README.rst 21 KB

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