RAM Usage of a process within Python script

Hi,

Is there a way to get the RAM usage of a process from within a python script that is essentially like the GetProcess command in deadlinecommand?

I mostly want to gather the current RAM usage of each of the slave processes on our farm. We have a memory leak issue and are still testing to make sure our upgrade to Deadline 7 will go smoothly; I have hopes it will possibly fix this issue. Until then, we need a faster way of checking and killing the slaves.

If there is not a way through Python, is there a way to use this…

“C:\Program Files\Thinkbox\Deadline6\bin\deadlinecommand.exe” GetProcess deadlineslave

…and output it to a text file or something with the slave name and process RAM usage?
…or to then force restart the machine if MemoryUsage is over a certain number?

Thanks,
A

Hello,

I do think this is possible, but I need to check with our devs to get an answer on this. I’ll get this info for you asap.

Are you looking to do this from pure Python (ie, running outside of the context of Deadline?) If so, that might be a bit tricky, there don’t seem to be many (good) ways for Python to query process info in a cross-platform way, without relying on third-party modules.

In the context of Deadline, though, (say by running the script with “deadlinecommand -executescript check_RAM.py”), it shouldn’t be too bad.

You have a couple options, the first being executing the GetProcess command through our scripting API:

from Deadline.Scripting import ClientUtils

result = ClientUtils.ExecuteCommandAndGetOutput( ["GetProcess", "deadlineslave"] )

Docs for this function are here

The other alternative is to directly use the .NET API (which uses Mono on non-windows) to get the process information:

from System.Diagnostics import Process

processes = Process.GetProcessesByName( "deadlineslave" )
for process in processes:
    print "ProcessName={0}".format( process.ProcessName )
    print "ProcessId={0}".format( process.Id )
    print "MemoryUsage={0} bytes".format( process.WorkingSet64 )
    print "VirtualMemory={0} bytes".format( process.PagedMemorySize64 )

This is basically all we’re doing behind the scenes in the “GetProcess” command, anyways. More info on the stuff available in the Process class can be found here.

Finally, if you want to go this route, you could also have DeadlineCommandBG spit out the output of the GetProcess command to a named file, like this:

DeadlineCommandBG.exe -GetProcess deadlineslave -outputfiles C:\command_output.txt C:\command_exit_code.txt

Hope this helps!

Cheers,
Jon

Hi Jon,

Thanks for the reply.

I’m looking to do this from inside Deadline.

I want it to be part of a larger script I have that’s currently looping through the slaves via “C:\Program Files\Thinkbox\Deadline6\bin\deadlinecommand.exe” -ExecuteScript

For the first option of executing GetProcess through API, I’m having trouble getting any sort of readable output.
If I execute a script from the right click script menu of a Slave, it outputs 2015-04-10 15:48:20: PYTHON: System.Diagnostics.Process[]
to the Console window.
If I put in as part of my larger script, it just creates a blank line in the python output.

The second option outputs the same thing.

The third option gives me:
Command exited with code: -1 (if I try to use DeadlineCommandBG.exe, even with the full path)

or with normal deadlinecommand.exe:
Command exited with code: 1
Command Stdout: Error: can not parse given string into a boolean value: -outputfiles
Parameter name: str (System.ArgumentException)

Thanks,
A