Is there a way to check to see if a pid corresponds to a valid process? I\'m getting a pid from a different source other than from os.getpid()
and I need to che
In Python 3.3+, you could use exception names instead of errno constants. Posix version:
import os
def pid_exists(pid):
if pid < 0: return False #NOTE: pid == 0 returns True
try:
os.kill(pid, 0)
except ProcessLookupError: # errno.ESRCH
return False # No such process
except PermissionError: # errno.EPERM
return True # Operation not permitted (i.e., process exists)
else:
return True # no error, we can send a signal to the process