1

Is there anyway to uniquely identify python processes running on Windows 7?

I am writing an installer that needs to kill a python process for the installation to continue, the problem is there are multiple python processes running and I need to make sure I kill the correct one!

4
  • Could you modify the code of that python process you want to kill? Commented Sep 2, 2013 at 17:25
  • see this stackoverflow.com/questions/12554176/… or this : stackoverflow.com/questions/16326529/… Commented Sep 2, 2013 at 17:26
  • I need to kill a specific python process as the installer updates this process. There are multiple python processes running ('pythonw.exe'). Commented Sep 2, 2013 at 17:30
  • @jossgray The only reliable way is to modify this specific process' code so that it will store its own PID somewhere on disk. Then you can easily kill it. Commented Sep 2, 2013 at 17:32

1 Answer 1

3

Presuming you know the command line your program was called with the first time, it might be something like this:

for p in psutil.get_process_list():
  if p.cmdline[0].endswith('pythonw.exe') and p.cmdline[1] == 'myscript.py':
    print p.pid

Be careful when traversing the cmdlines of various programs though, they have different numbers of elements and can't be reliably unpacked (though I think python 3 has some support for variable length sequence unpacking). Anyway, worth a try.

Sign up to request clarification or add additional context in comments.

2 Comments

len(p.cmdline) would return number of elements in the list on any Python version.
Definitely, I was just warning against prog, arg = p.cmdline blindly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.