Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell process id within Python

I am working with a cluster system over linux (www.mosix.org) that allows me to run jobs and have the system run them on different computers. Jobs are run like so:

mosrun ls &

This will naturally create the process and run it on the background, returning the process id, like so:

[1] 29199

Later it will return. I am writing a Python infrastructure that would run jobs and control them. For that I want to run jobs using the mosrun program as above, and save the process ID of the spawned process (29199 in this case). This naturally cannot be done using os.system or commands.getoutput, as the printed ID is not what the process prints to output... Any clues?

Edit:

Since the python script is only meant to initially run the script, the scripts need to run longer than the python shell. I guess it means the mosrun process cannot be the script's child process. Any suggestions?

Thanks

like image 554
R S Avatar asked May 21 '10 18:05

R S


People also ask

How can I see process ID?

Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column.

How do you display a process in 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.

What does process () do in Python?

In Python, a process is an instance of the Python interpreter that executes Python code. In Python, the first process created when we run our program is called the 'MainProcess'. It is also a parent process and may be called the main parent process. The main process will create the first child process or processes.


2 Answers

Use subprocess module. Popen instances have a pid attribute.

like image 184
Cat Plus Plus Avatar answered Sep 30 '22 11:09

Cat Plus Plus


Looks like you want to ensure the child process is daemonized -- PEP 3143, which I'm pointing to, documents and points to a reference implementation for that, and points to others too.

Once your process (still running Python code) is daemonized, be it by the means offered in PEP 3143 or others, you can os.execl (or other os.exec... function) your target code -- this runs said target code in exactly the same process which we just said is daemonized, and so it keeps being daemonized, as desired.

The last step cannot use subprocess because it needs to run in the same (daemonized) process, overlaying its executable code -- exactly what os.execl and friends are for.

The first step, before daemonization, might conceivably be done via subprocess, but that's somewhat inconvenient (you need to put the daemonize-then-os.exec code in a separate .py): most commonly you'd just want to os.fork and immediately daemonize the child process.

subprocess is quite convenient as a mostly-cross-platform way to run other processes, but it can't really replace Unix's good old "fork and exec" approach for advanced uses (such as daemonization, in this case) -- which is why it's a good thing that the Python standard library also lets you do the latter via those functions in module os!-)

like image 44
Alex Martelli Avatar answered Sep 30 '22 11:09

Alex Martelli