I am implementing a online judge in python. The judge is to run on a UNIX machine. To enforce memory limits I am using ulimit and to run a program I am using subprocess.Popen() . I want to calculate the exact memory usage. How can I do so?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 150 |
I am implementing a online judge in python. The judge is to run on a UNIX machine. To enforce memory limits I am using ulimit and to run a program I am using subprocess.Popen() . I want to calculate the exact memory usage. How can I do so?
Название |
---|
Use
resource.getrusage()
:http://docs.python.org/3/library/resource.html#resource.getrusage
The maximum used memory is
ru_maxrss
.I used ~~~~~
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print getrusage(resource.RUSAGE_CHILDREN).ru_maxrss
~~~~~but the output is always zero am I doing something wrong. cmd is the command to be executed.
You need to wait for the child first: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getrusage.html
Okay.. Now I am using
~~~~~
But still I am getting about the same value(~3430) for any program that I am executing. But the programs: https://ideone.com/KK1rak and http://ideone.com/3HfV9f take different memory according to ideone.
That is because you measure maxrss of the shell process and not of your program. You have to use
shell=False
.Also, it seems that resource usage is cumulative, so if you want to run multiple programs and obtain individual data about them, you'd have to fork a Python process for each time.
I changed to this.
with open('in.txt','r') as infile, open('out.txt', 'w') as outfile:
Res= getrusage(resource.RUSAGE_CHILDREN)
print Res.ru_maxrss
Still I am getting a constant value(~5060).
Now it seems you forgot the wait... This works for me:
EDIT: I did not notice you changed
Popen
tocheck_call
, it will wait for the process indeed.Maybe you could take a look at this opensource online judge (written in Python) and see how they do it :)