简体   繁体   English

使用Python检查Windows是否存在PID而不需要库

[英]Check if PID exists on Windows with Python without requiring libraries

Is there a way to check if a PID exists on Windows with Python without requiring libraries? 有没有办法检查Windows上是否存在PID而不需要库? How to? 如何?

This is solved with a little cup of WINAPI. 用一小杯WINAPI解决了这个问题。

def pid_running(pid):
    import ctypes
    kernel32 = ctypes.windll.kernel32
    SYNCHRONIZE = 0x100000

    process = kernel32.OpenProcess(SYNCHRONIZE, 0, pid)
    if process != 0:
        kernel32.CloseHandle(process)
        return True
    else:
        return False

This works on my system.. 这适用于我的系统..

>>> import subprocess
>>> out = subprocess.check_output(["tasklist","/fi","PID eq 1234"]).strip()
>>> if out == "INFO: No tasks are running which match the specified criteria.":
...   print "No such PID :D"
...
No such PID :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM