README.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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?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?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. </div>
  124. <sup><a href="https://github.com/sponsors/giampaolo">add your avatar</a></sup>
  125. Contributing
  126. ============
  127. See `contributing guidelines <https://github.com/giampaolo/psutil/blob/master/CONTRIBUTING.md>`__.
  128. Example usages
  129. ==============
  130. This represents pretty much the whole psutil API.
  131. CPU
  132. ---
  133. .. code-block:: python
  134. >>> import psutil
  135. >>>
  136. >>> psutil.cpu_times()
  137. 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)
  138. >>>
  139. >>> for x in range(3):
  140. ... psutil.cpu_percent(interval=1)
  141. ...
  142. 4.0
  143. 5.9
  144. 3.8
  145. >>>
  146. >>> for x in range(3):
  147. ... psutil.cpu_percent(interval=1, percpu=True)
  148. ...
  149. [4.0, 6.9, 3.7, 9.2]
  150. [7.0, 8.5, 2.4, 2.1]
  151. [1.2, 9.0, 9.9, 7.2]
  152. >>>
  153. >>> for x in range(3):
  154. ... psutil.cpu_times_percent(interval=1, percpu=False)
  155. ...
  156. 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)
  157. 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)
  158. 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)
  159. >>>
  160. >>> psutil.cpu_count()
  161. 4
  162. >>> psutil.cpu_count(logical=False)
  163. 2
  164. >>>
  165. >>> psutil.cpu_stats()
  166. scpustats(ctx_switches=20455687, interrupts=6598984, soft_interrupts=2134212, syscalls=0)
  167. >>>
  168. >>> psutil.cpu_freq()
  169. scpufreq(current=931.42925, min=800.0, max=3500.0)
  170. >>>
  171. >>> psutil.getloadavg() # also on Windows (emulated)
  172. (3.14, 3.89, 4.67)
  173. Memory
  174. ------
  175. .. code-block:: python
  176. >>> psutil.virtual_memory()
  177. svmem(total=10367352832, available=6472179712, percent=37.6, used=8186245120, free=2181107712, active=4748992512, inactive=2758115328, buffers=790724608, cached=3500347392, shared=787554304)
  178. >>> psutil.swap_memory()
  179. sswap(total=2097147904, used=296128512, free=1801019392, percent=14.1, sin=304193536, sout=677842944)
  180. >>>
  181. Disks
  182. -----
  183. .. code-block:: python
  184. >>> psutil.disk_partitions()
  185. [sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,nosuid', maxfile=255, maxpath=4096),
  186. sdiskpart(device='/dev/sda2', mountpoint='/home', fstype='ext', opts='rw', maxfile=255, maxpath=4096)]
  187. >>>
  188. >>> psutil.disk_usage('/')
  189. sdiskusage(total=21378641920, used=4809781248, free=15482871808, percent=22.5)
  190. >>>
  191. >>> psutil.disk_io_counters(perdisk=False)
  192. 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)
  193. >>>
  194. Network
  195. -------
  196. .. code-block:: python
  197. >>> psutil.net_io_counters(pernic=True)
  198. {'eth0': netio(bytes_sent=485291293, bytes_recv=6004858642, packets_sent=3251564, packets_recv=4787798, errin=0, errout=0, dropin=0, dropout=0),
  199. 'lo': netio(bytes_sent=2838627, bytes_recv=2838627, packets_sent=30567, packets_recv=30567, errin=0, errout=0, dropin=0, dropout=0)}
  200. >>>
  201. >>> psutil.net_connections(kind='tcp')
  202. [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),
  203. 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),
  204. ...]
  205. >>>
  206. >>> psutil.net_if_addrs()
  207. {'lo': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast='127.0.0.1', ptp=None),
  208. snicaddr(family=<AddressFamily.AF_INET6: 10>, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None, ptp=None),
  209. snicaddr(family=<AddressFamily.AF_LINK: 17>, address='00:00:00:00:00:00', netmask=None, broadcast='00:00:00:00:00:00', ptp=None)],
  210. 'wlan0': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='192.168.1.3', netmask='255.255.255.0', broadcast='192.168.1.255', ptp=None),
  211. snicaddr(family=<AddressFamily.AF_INET6: 10>, address='fe80::c685:8ff:fe45:641%wlan0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None),
  212. snicaddr(family=<AddressFamily.AF_LINK: 17>, address='c4:85:08:45:06:41', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]}
  213. >>>
  214. >>> psutil.net_if_stats()
  215. {'lo': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=65536, flags='up,loopback,running'),
  216. 'wlan0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=100, mtu=1500, flags='up,broadcast,running,multicast')}
  217. >>>
  218. Sensors
  219. -------
  220. .. code-block:: python
  221. >>> import psutil
  222. >>> psutil.sensors_temperatures()
  223. {'acpitz': [shwtemp(label='', current=47.0, high=103.0, critical=103.0)],
  224. 'asus': [shwtemp(label='', current=47.0, high=None, critical=None)],
  225. 'coretemp': [shwtemp(label='Physical id 0', current=52.0, high=100.0, critical=100.0),
  226. shwtemp(label='Core 0', current=45.0, high=100.0, critical=100.0)]}
  227. >>>
  228. >>> psutil.sensors_fans()
  229. {'asus': [sfan(label='cpu_fan', current=3200)]}
  230. >>>
  231. >>> psutil.sensors_battery()
  232. sbattery(percent=93, secsleft=16628, power_plugged=False)
  233. >>>
  234. Other system info
  235. -----------------
  236. .. code-block:: python
  237. >>> import psutil
  238. >>> psutil.users()
  239. [suser(name='giampaolo', terminal='pts/2', host='localhost', started=1340737536.0, pid=1352),
  240. suser(name='giampaolo', terminal='pts/3', host='localhost', started=1340737792.0, pid=1788)]
  241. >>>
  242. >>> psutil.boot_time()
  243. 1365519115.0
  244. >>>
  245. Process management
  246. ------------------
  247. .. code-block:: python
  248. >>> import psutil
  249. >>> psutil.pids()
  250. [1, 2, 3, 4, 5, 6, 7, 46, 48, 50, 51, 178, 182, 222, 223, 224, 268, 1215,
  251. 1216, 1220, 1221, 1243, 1244, 1301, 1601, 2237, 2355, 2637, 2774, 3932,
  252. 4176, 4177, 4185, 4187, 4189, 4225, 4243, 4245, 4263, 4282, 4306, 4311,
  253. 4312, 4313, 4314, 4337, 4339, 4357, 4358, 4363, 4383, 4395, 4408, 4433,
  254. 4443, 4445, 4446, 5167, 5234, 5235, 5252, 5318, 5424, 5644, 6987, 7054,
  255. 7055, 7071]
  256. >>>
  257. >>> p = psutil.Process(7055)
  258. >>> p
  259. psutil.Process(pid=7055, name='python3', status='running', started='09:04:44')
  260. >>> p.pid
  261. 7055
  262. >>> p.name()
  263. 'python3'
  264. >>> p.exe()
  265. '/usr/bin/python3'
  266. >>> p.cwd()
  267. '/home/giampaolo'
  268. >>> p.cmdline()
  269. ['/usr/bin/python3', 'main.py']
  270. >>>
  271. >>> p.ppid()
  272. 7054
  273. >>> p.parent()
  274. psutil.Process(pid=4699, name='bash', status='sleeping', started='09:06:44')
  275. >>> p.parents()
  276. [psutil.Process(pid=4699, name='bash', started='09:06:44'),
  277. psutil.Process(pid=4689, name='gnome-terminal-server', status='sleeping', started='0:06:44'),
  278. psutil.Process(pid=1, name='systemd', status='sleeping', started='05:56:55')]
  279. >>> p.children(recursive=True)
  280. [psutil.Process(pid=29835, name='python3', status='sleeping', started='11:45:38'),
  281. psutil.Process(pid=29836, name='python3', status='waking', started='11:43:39')]
  282. >>>
  283. >>> p.status()
  284. 'running'
  285. >>> p.create_time()
  286. 1267551141.5019531
  287. >>> p.terminal()
  288. '/dev/pts/0'
  289. >>>
  290. >>> p.username()
  291. 'giampaolo'
  292. >>> p.uids()
  293. puids(real=1000, effective=1000, saved=1000)
  294. >>> p.gids()
  295. pgids(real=1000, effective=1000, saved=1000)
  296. >>>
  297. >>> p.cpu_times()
  298. pcputimes(user=1.02, system=0.31, children_user=0.32, children_system=0.1, iowait=0.0)
  299. >>> p.cpu_percent(interval=1.0)
  300. 12.1
  301. >>> p.cpu_affinity()
  302. [0, 1, 2, 3]
  303. >>> p.cpu_affinity([0, 1]) # set
  304. >>> p.cpu_num()
  305. 1
  306. >>>
  307. >>> p.memory_info()
  308. pmem(rss=10915840, vms=67608576, shared=3313664, text=2310144, lib=0, data=7262208, dirty=0)
  309. >>> p.memory_full_info() # "real" USS memory usage (Linux, macOS, Win only)
  310. pfullmem(rss=10199040, vms=52133888, shared=3887104, text=2867200, lib=0, data=5967872, dirty=0, uss=6545408, pss=6872064, swap=0)
  311. >>> p.memory_percent()
  312. 0.7823
  313. >>> p.memory_maps()
  314. [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),
  315. 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),
  316. 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),
  317. 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),
  318. ...]
  319. >>>
  320. >>> p.io_counters()
  321. pio(read_count=478001, write_count=59371, read_bytes=700416, write_bytes=69632, read_chars=456232, write_chars=517543)
  322. >>>
  323. >>> p.open_files()
  324. [popenfile(path='/home/giampaolo/monit.py', fd=3, position=0, mode='r', flags=32768),
  325. popenfile(path='/var/log/monit.log', fd=4, position=235542, mode='a', flags=33793)]
  326. >>>
  327. >>> p.connections(kind='tcp')
  328. [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'),
  329. 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')]
  330. >>>
  331. >>> p.threads()
  332. [pthread(id=5234, user_time=22.5, system_time=9.2891),
  333. pthread(id=5237, user_time=0.0707, system_time=1.1)]
  334. >>>
  335. >>> p.num_threads()
  336. 4
  337. >>> p.num_fds()
  338. 8
  339. >>> p.num_ctx_switches()
  340. pctxsw(voluntary=78, involuntary=19)
  341. >>>
  342. >>> p.nice()
  343. 0
  344. >>> p.nice(10) # set
  345. >>>
  346. >>> p.ionice(psutil.IOPRIO_CLASS_IDLE) # IO priority (Win and Linux only)
  347. >>> p.ionice()
  348. pionice(ioclass=<IOPriority.IOPRIO_CLASS_IDLE: 3>, value=0)
  349. >>>
  350. >>> p.rlimit(psutil.RLIMIT_NOFILE, (5, 5)) # set resource limits (Linux only)
  351. >>> p.rlimit(psutil.RLIMIT_NOFILE)
  352. (5, 5)
  353. >>>
  354. >>> p.environ()
  355. {'LC_PAPER': 'it_IT.UTF-8', 'SHELL': '/bin/bash', 'GREP_OPTIONS': '--color=auto',
  356. 'XDG_CONFIG_DIRS': '/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg',
  357. ...}
  358. >>>
  359. >>> p.as_dict()
  360. {'status': 'running', 'num_ctx_switches': pctxsw(voluntary=63, involuntary=1), 'pid': 5457, ...}
  361. >>> p.is_running()
  362. True
  363. >>> p.suspend()
  364. >>> p.resume()
  365. >>>
  366. >>> p.terminate()
  367. >>> p.kill()
  368. >>> p.wait(timeout=3)
  369. <Exitcode.EX_OK: 0>
  370. >>>
  371. >>> psutil.test()
  372. USER PID %CPU %MEM VSZ RSS TTY START TIME COMMAND
  373. root 1 0.0 0.0 24584 2240 Jun17 00:00 init
  374. root 2 0.0 0.0 0 0 Jun17 00:00 kthreadd
  375. ...
  376. giampaolo 31475 0.0 0.0 20760 3024 /dev/pts/0 Jun19 00:00 python2.4
  377. giampaolo 31721 0.0 2.2 773060 181896 00:04 10:30 chrome
  378. root 31763 0.0 0.0 0 0 00:05 00:00 kworker/0:1
  379. >>>
  380. Further process APIs
  381. --------------------
  382. .. code-block:: python
  383. >>> import psutil
  384. >>> for proc in psutil.process_iter(['pid', 'name']):
  385. ... print(proc.info)
  386. ...
  387. {'pid': 1, 'name': 'systemd'}
  388. {'pid': 2, 'name': 'kthreadd'}
  389. {'pid': 3, 'name': 'ksoftirqd/0'}
  390. ...
  391. >>>
  392. >>> psutil.pid_exists(3)
  393. True
  394. >>>
  395. >>> def on_terminate(proc):
  396. ... print("process {} terminated".format(proc))
  397. ...
  398. >>> # waits for multiple processes to terminate
  399. >>> gone, alive = psutil.wait_procs(procs_list, timeout=3, callback=on_terminate)
  400. >>>
  401. Windows services
  402. ----------------
  403. .. code-block:: python
  404. >>> list(psutil.win_service_iter())
  405. [<WindowsService(name='AeLookupSvc', display_name='Application Experience') at 38850096>,
  406. <WindowsService(name='ALG', display_name='Application Layer Gateway Service') at 38850128>,
  407. <WindowsService(name='APNMCP', display_name='Ask Update Service') at 38850160>,
  408. <WindowsService(name='AppIDSvc', display_name='Application Identity') at 38850192>,
  409. ...]
  410. >>> s = psutil.win_service_get('alg')
  411. >>> s.as_dict()
  412. {'binpath': 'C:\\Windows\\System32\\alg.exe',
  413. 'description': 'Provides support for 3rd party protocol plug-ins for Internet Connection Sharing',
  414. 'display_name': 'Application Layer Gateway Service',
  415. 'name': 'alg',
  416. 'pid': None,
  417. 'start_type': 'manual',
  418. 'status': 'stopped',
  419. 'username': 'NT AUTHORITY\\LocalService'}
  420. Projects using psutil
  421. =====================
  422. Here's some I find particularly interesting:
  423. - https://github.com/google/grr
  424. - https://github.com/facebook/osquery/
  425. - https://github.com/nicolargo/glances
  426. - https://github.com/aristocratos/bpytop
  427. - https://github.com/Jahaja/psdash
  428. - https://github.com/ajenti/ajenti
  429. - https://github.com/home-assistant/home-assistant/
  430. Portings
  431. ========
  432. - Go: https://github.com/shirou/gopsutil
  433. - C: https://github.com/hamon-in/cpslib
  434. - Rust: https://github.com/rust-psutil/rust-psutil
  435. - Nim: https://github.com/johnscillieri/psutil-nim