Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing an ALREADY running process, with Python

Question: Is there a way, using Python, to access the stdout of a running process? This process has not been started by Python.

Context: There is a program called mayabatch, that renders out images from 3D Maya scene files. If I were to run the program from the command line I would see progress messages from mayabatch. Sometimes, artists close these windows, leaving the progress untracable until the program finishes. That led me along this route of trying to read its stdout after it's been spawned by a foreign process.

Background:

  • OS: Windows 7 64-bit

My research so far: I have only found questions and answers of how to do this if it was a subprocess, using the subprocess module. I also looked briefly into psutil, but I could not find any way to read a process' stdout.

Any help would be really appreciated. Thank you.

like image 546
kartikg3 Avatar asked Dec 19 '14 20:12

kartikg3


People also ask

How do you check if a process is already running in Python?

Answer #1: Drop a pidfile somewhere (e.g. /tmp). Then you can check to see if the process is running by checking to see if the PID in the file exists. Don't forget to delete the file when you shut down cleanly, and check for it when you start up.

How do I see processes in Python?

getpid() method in Python is used to get the process ID of the current process.

How do I list running processes in Windows using Python?

We would be using the WMI. Win32_Process function in order to get the list of running processes on the system. Then we called the function WMI. Win32_Process() to get the running processes, iterated through each process and stored in variable process.

How do you start a process in python?

How to Start a Process in Python? To start a new process, or in other words, a new subprocess in Python, you need to use the Popen function call. It is possible to pass two parameters in the function call. The first parameter is the program you want to start, and the second is the file argument.


1 Answers

I don't think you can get to the stdout of a process outside of the code that created it

The lazy way to is just to pipe the output of mayabatch to a text file, and then poll the text file periodically in your own code so it's under your control, rather than forcing you to wait on the pipe (which is especially hard on Windows, since Windows select doesn't work with the pipes used by subprocess.

I think this is what maya does internally too: by default mayaBatch logs its results to a file called mayaRenderLog.txt in the user's Maya directory.

If you're running mayabatch from the command line or a bat file, you can funnel stdout to a file with a > character:

  mayabatch.exe "file.ma" > log.txt

You should be able to poll that text file from the outside using standard python as long as you only open it for reading. The advantage of doing it this way is that you control the frequency at which you check the file.

OTOH If you're doing it from python, it's a little tougher unless you don't mind having your python script idled until the mayabatch completes. The usual subprocess recipe, which uses popen.communicate() is going to wait for an end-of-process return code:

 test = subprocess.Popen(["mayabatch.exe","filename.mb"], stdout=subprocess.PIPE)
 print test.communicate()[0]

works but won't report until the process dies. But you calling readlines on the process's stdout will trigger the process and report it one line at a time:

 test = subprocess.Popen(["mayabatch.exe","filename.mb"], stdout=subprocess.PIPE)
 reader = iter(test.subprocess.readlines, "")
 for line in reader:
     print line

More discussion here

like image 88
theodox Avatar answered Sep 27 '22 00:09

theodox