Open giampaolo opened 7 years ago
Not sure if it's relevant to this issue (Windows only?) or related to a different one as this is a Linux-based comment, but there are additional limitations in cgroup
on Linux which are used by Docker containers. The relevant file is /sys/fs/cgroup/memory/memory.limit_in_bytes
.
Existing resource limits capabilities
Right now it is possible to limit the resources of a certain process in many ways on both UNIX and Windows:
Process.nice()
)Process.ionice()
)Process.cpu_affinity()
)Linux (UPDATE: and FreeBSD) has a fourth option,
Process.rlimit()
, see here. It appears the same can be done on (recent verions of) Windows, see python-ideas post. Here's some findings.CPU
In here it is suggested to use
NtSetInformationProcess
in order to set CPU percent limit (!) but it's an undocumented call. A modern alternative (Windows 8.1 +) is SetInformationJobObject, which appears to be the key to all resource limit functionalities. E.g.SetInformationJobObject
+JobObjectCpuRateControlInformation
lets you set a CPU percent limit, which is quite impressive. Here is a C++ cmdline tool using it. This is important as it shows it accepts a PID, meaning this can be done on a per-process basis and fit into thepsutil.Process(pid)
model.Memory
Looks like JOBOBJECT_EXTENDED_LIMIT_INFORMATION lets you a generic virtual memory limit for the process. JOBOBJECT_BASIC_LIMIT_INFORMATION lets you set min/max working set size (
Process.memory_info().rss
in psutil terms). See SetProcessWorkingSetSize.Network
JOBOBJECT_NET_RATE_CONTROL_INFORMATION limits the outgoing network bandwidth (max bytes - what about incoming?).
Disk IO
JOBOBJECT_LIMIT_VIOLATION_INFORMATION limits bytes read and written from/to disk.
Others
There is a command line tool by daemontools which also gives an idea on what capabilities are: https://cr.yp.to/daemontools/softlimit.html It shows shows other interesting things can be done amongst which:
API
I suppose that we can probably reuse Linux's
Process.rlimit
method and its signature, which isrlimit(resource, limits=None)
and serves both "get" and "set" usage. Hypothetical idea:When more than one argument is supposed to be passed we can use a tuple as in (still hypothetical):