giampaolo / psutil

Cross-platform lib for process and system monitoring in Python
BSD 3-Clause "New" or "Revised" License
10.08k stars 1.37k forks source link

[FreeBSD] speed up net_connections() #2343

Closed giampaolo closed 6 months ago

giampaolo commented 6 months ago

Summary

Description

Modify psutil.net_connections() to retrieving unnecessary connection types unless explicitly asked. E.g., on an IDLE system with few IPv6 connections this will run around 4 times faster:

import psutil, time
started = time.monotonic()
for x in range(1000):
    psutil.net_connections("tcp6")
print(f"completed in {(time.monotonic() - started):.4f} secs")

Before all connection types (TCP, UDP, UNIX) were retrieved internally, even if only a portion was returned.