How to Change Effective Process Name in Python

Is there a way to change effective process name in Python?

Simply put, there's no portable way. You'll have to test for the system and use the preferred method for that system.

Further, I'm confused about what you mean by process names on Windows.

Do you mean a service name? I presume so, because nothing else really makes any sense (at least to my non-Windows using brain).

If so, you need to use Tim Golden's WMI interface and call the .Change method on the service... at least according to his tutorial.

For Linux none of the methods I found worked except for this poorly packaged module that sets argv[0] for you.

I don't even know if this will work on BSD variants (which does have a setproctitle system call). I'm pretty sure argv[0] won't work on Solaris.

changing the process name of a python script

http://code.google.com/p/procname/

Sample usage:

# Lets rename:    
>>> procname.setprocname('My super name')

# Lets check. Press Ctrl+Z
user@comp:~/procname$ ps

PID TTY TIME CMD

13016 pts/2 00:00:00 bash

13128 pts/2 00:00:00 My super name <-- it's here

It will only work on systems where prctl system call is present and supports PR_SET_NAME command.

Change process name of Python script

There's no nice way that I've found to change the name of a running process in Windows, but you can create small .exe stubs with ExeMaker rather than resorting to py2exe packaging or copying the interpreter

The exe stub uses its own module name to calculate the .py script invoked. You should be able to use an exe resource editor to change the icon.

python multiprocessing - access the process name inside the function called with Process.start(target=func)

You can use the current_process function:

from multiprocessing import Process, current_process

def somefunc():
print current_process().name

if __name__ == '__main__':
p = Process(target=somefunc)
p.start()
print p.name

Python Get windowtitle from Process ID or Process Name

import win32gui
import win32process
import psutil
import ctypes

EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible

def getProcessIDByName():
qobuz_pids = []
process_name = "Qobuz.exe"

for proc in psutil.process_iter():
if process_name in proc.name():
qobuz_pids.append(proc.pid)

return qobuz_pids

def get_hwnds_for_pid(pid):
def callback(hwnd, hwnds):
#if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
_, found_pid = win32process.GetWindowThreadProcessId(hwnd)

if found_pid == pid:
hwnds.append(hwnd)
return True
hwnds = []
win32gui.EnumWindows(callback, hwnds)
return hwnds

def getWindowTitleByHandle(hwnd):
length = GetWindowTextLength(hwnd)
buff = ctypes.create_unicode_buffer(length + 1)
GetWindowText(hwnd, buff, length + 1)
return buff.value

def getQobuzHandle():
pids = getProcessIDByName()

for i in pids:
hwnds = get_hwnds_for_pid(i)
for hwnd in hwnds:
if IsWindowVisible(hwnd):
return hwnd

if __name__ == '__main__':
qobuz_handle = getQobuzHandle()

I solved it using this piece of code
With this I get the window handle of the qobuz window if it's open, else it's none



Related Topics



Leave a reply



Submit