Reputation: 118560
What are the Windows equivalents to the resource limit mechanisms exposed on Unix systems by Python's resource
module, and POSIX setrlimit
?
Specifically, I'm limiting processor time for a child process to several seconds. If it hasn't completed within the constraint, it's terminated.
Upvotes: 10
Views: 3800
Reputation: 43505
AFAIK, there is no portable way of getting information about the amount of processor time used by a child process in Python. But what subprocess
module does (assuming you're starting the child with subprocess.Popen
, which is recommended) give you is the process ID of the child process in Popen.pid
. What you could do on Windows is run tasklist
(see manual) using subprocess.check_output
repeatedly and extract the info about the child proces from its output, using the PID as a filter.
As soon as the child process has has enough CPU time and if you used subprocess.Popen()
to start the child process, you could use the Popen.kill
method to kill it.
But I think it would be easier to kill the child process after after a specified number of seconds of wall time using a timer. Because if the child process hangs without using CPU time (for whatever reason), so does your python program that is waiting for it to consume CPU time.
Upvotes: 4