Getting the current process ID.

Is there a way to get the process ID of the current RUNNING process? I found this script but it still doesn’t work for me. If there is a problem, please correct me.

import os
def findPID(exename):
    a = os.popen4('tasklist /FI "IMAGENAME eq '+exename+'"')
    return (info[0],info[1])
print soft,' --> ',pid

It’s much easier than that.

import os
print os.getpid()


import os
print os.getpid()

Sorry but that code only gets the pid of blender (or the process that owns the code). I want to get the pid of the currently running program.

The python code you mean? The interpreter is embedded into Blender, it runs in the same process.

In any case, by definition, getpid returns the pid of the current running process (where the code is executed), so it cannot be the “wrong” answer unless what you want is not the pid of the process of the python interpreter.


I would like to get the pid of all the processes and then out of that list, pick the pid of the current running window.

Define “current running window”?

os.getpid is always the currently running process, so you’ll have to tell us what you mean by “current running window”, especially since it doesn’t seem to fit to “current running process”.

Alternatively, you could tell us what you want to do with that pid, then, we can probably help you better.


This is what I mean by “Current Window”.
What I am trying to do is to get the pid of blender with os.getpid() and then get the pid of the current window and compare the two pids to see if they match.

The only way to do that would be to wrap win32 (Windows’ API) calls and even with that, I’m not even sure it would be possible (not sure if the API has capacities for that).

If it has, there’s a couple of ways you could do it:

1- Write a small standalone program that returns the PID of the active window’s process (as termination code or write to stdout or whatnot).

2- Create a Python/C module that wraps the win32 calls and does the job you need as a Python module.

The first solution is much easier, obviously.

For the actual win32 calls, you’ll have to hunt around some more though. :frowning:


I happen to have created a python extension module ‘win32procinfo’ which might do what you want, or at least it might be useful to you if you want to modify for your own use.
I used it in my Lightflow script from several years ago. The source is quite small and simple really, but I don’t know if it would still work today, considering I used win98 at the time. Can’t test it anymore, since I lost my window partition recently due to hardware problems.
But if you want the source, PM me with a mail addres so I can send it to you.
In any case, it has functions for returning all currently running processes, testing for activity, ‘kill’ by handle or id, and getting the status of a program.

btw, you can also get a process ID (or handle on win) if you use one of the spawn() funcs to execute an external program, but maybe that is of no use to you.

I would be surprised though if there is not somewhere some sort of python support nowadays for this sort of thing.