Problem Description
The Rlimit method using Psutil on centos6.5 reported the following error
>>> import psutil
>>> p=psutil. Process ()
>>> p.rlimit (psutil. RLIMIT_CPU)
Traceback (most recent):
File "<stdin>", line 1, in <module>
Attributeerror: ' Process ' object has no attribute ' rlimit '
problem Reason
First check the Psutil version, is the latest version 4.1.0, the Rlimit function should already be included in the library, so the library version is excluded too low issue.
And then see a description in the {path_to_python}/site-packages/psutil/tests/test_linux.py file that explains the problem well, see the code snippet below
@unittest. Skipunless (
get_kernel_version () >= (2, 6,),
"Prlimit () Not available on" Linux kernel Versio N ")
def test_prlimit_availability (self):
# Prlimit () should is available starting from kernel 2.6.36
p = Psut Il. Process (Os.getpid ())
P.rlimit (psutil. Rlimit_nofile)
# if Prlimit () is supported *at least* this constants should
Note that the Rlimit function is available only in Linux kernel 2.6.36 and above, so decisively view the system kernel version
$ uname-a
Linux fanbinlinux 2.6.32-573.22.1.el6.x86_64 #1 SMP Wed Mar 03:35:39 UTC 2016 x86_64 x86_64 x86_64 Inux
The original is 2.6.32, OK, life back, but finally found the cause of the problem. Solutions Upgrade Kernel
Oh, this is to tease you to play the solution, many situations users can not allow you to upgrade the kernel, of course, if allowed, this is the simplest solution. Swap Resource Library
Looking at the official Python documentation and discovering that the Getrlimit and Setrlimit methods can be implemented using the standard library resource, the difference between resource library and Psutil library is that the resource library can only set or query Rlimit for the current process. And Psutil can be targeted at any process.
The following is the use of the resource library
Import Resource
Resource.getrlimit (resource. RLIMIT_CPU)
Resource.setrlimit (Resource). RLIMIT_CPU, XXX)
Reference https://docs.python.org/2/library/resource.html
Reprint please indicate this address in the form of link
This article address: http://blog.csdn.net/kongxx/article/details/51189373