Simple way to check if Process exits using PID

52 views
Skip to first unread message

Sai Mahidhar Vanumu

unread,
Feb 18, 2023, 10:48:51 AM2/18/23
to psutil
Hello there,
Is there a simple way to find if process is running or not running using pid alone. 
There is psutil.pid_exits() functions which does work indeed. However just for a sake of one feature installing psutil seems to be meaningless. 

Is there a way to find out if process exist using pid without psutil.
I have tried using ctypes below code, from stackoverflow, 

the code:
import ctypes
PROCESS_QUERY_INFROMATION = 0x1000
def checkPid(pid):
processHandle = ctypes.windll.kernel32.OpenProcess(PROCESS_QUERY_INFROMATION, 0,pid)
if processHandle == 0:
return False
else:
ctypes.windll.kernel32.CloseHandle(processHandle)
return True

But it sometimes return True even if process does not exists. Ps util correctly returns False.

I have tried using win32process and querying via wmi win32process but that is too much cpu utilization 



Giampaolo Rodola'

unread,
Feb 18, 2023, 11:11:05 AM2/18/23
to psu...@googlegroups.com, mahi...@fusiontech.in
> is there a way to find out if process exist using pid without psutil.

On UNIX it's easy:
On Windows it's more complex. According to my experience, using OpenProcess alone is not reliable. In some cases you're forced to iterate over all existing PIDs, see:
Some solutions that come to mind:

1) Replicating that same logic by using ctypes is hard. You may have more luck searching how to list all PIDs with ctypes, and just ignore OpenProcess. It will be slower than replicating the same psutil algo, but probably a lot easier .

2) Parse a subprocess output. I suppose "tasklist" is the command you're looking for: https://superuser.com/questions/914782/how-do-you-list-all-processes-on-the-command-line-in-windows

3) WMI is another alternative. You can use it both from the cmdline and from python. via the "wmi" module. Here's how to get PIDs: https://github.com/giampaolo/psutil/blob/bea3cf2d16899251b4b5f6b2609db9881645ea2d/psutil/tests/test_windows.py#L179-L185

Note that all of these solutions will be considerably slower than psutil.



--
You received this message because you are subscribed to the "Python process utilities (psutil)" project group:
https://groups.google.com/g/psutil
To post to this group, send email to psu...@googlegroups.com
To unsubscribe from this group, send email to psutil-un...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "psutil" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psutil+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psutil/ca06504e-eb7c-45c3-b598-251fd4844034n%40googlegroups.com.


--
Giampaolo - gmpy.dev

Giampaolo Rodola'

unread,
Feb 18, 2023, 1:32:51 PM2/18/23
to Sai Mahidhar Vanumu, psu...@googlegroups.com
I don't know. My main point about speed is about the **algorithm**. 
psutil is supposed to be faster than the other solutions because it attempts to use OpenProcess first. 
If OpenProcess succeeds it means the process is alive (fast and O(1)), if it fails it has to iterate over all PIDs (slower and O(n)).

On Sat, Feb 18, 2023 at 7:25 PM Sai Mahidhar Vanumu <mahi...@fusiontech.in> wrote:
Hi thanks for suggestions, it had suddenly came to my mind when I looked at psapi.h in file you have shared as link (utils.c)

Using ctypes and importing psapi dll is indeed doable. 

Sorry for follow up question, Is Using psapi with ctypes is still slow?? I am pretty sure tasklist wmi and win32process are damm slow, but what about psapi with ctypes. ?


--
Giampaolo - gmpy.dev

Sai Mahidhar Vanumu

unread,
Feb 18, 2023, 1:33:29 PM2/18/23
to Giampaolo Rodola', psu...@googlegroups.com
Hi thanks for suggestions, it had suddenly came to my mind when I looked at psapi.h in file you have shared as link (utils.c)

Using ctypes and importing psapi dll is indeed doable. 

Sorry for follow up question, Is Using psapi with ctypes is still slow?? I am pretty sure tasklist wmi and win32process are damm slow, but what about psapi with ctypes. ?

On Sat, Feb 18, 2023, 9:41 PM Giampaolo Rodola' <g.ro...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages