Skip to content

Commit

Permalink
add utils.is_running_linux, utils.is_running_win32
Browse files Browse the repository at this point in the history
  • Loading branch information
lidong committed Jun 19, 2024
1 parent 5400df1 commit 25419de
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 0.0.7 (2024-06-19)
1. add `utils.is_running_linux`, `utils.is_running_win32`


### 0.0.6 (2024-06-19)
1. add `utils.xor_encode_decode`
2. add `utils.is_running`, `utils.lock_pid_file`
Expand Down
2 changes: 1 addition & 1 deletion morebuiltins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.0.6"
__version__ = "0.0.7"
__all__ = [
"morebuiltins.utils",
"morebuiltins.functools",
Expand Down
31 changes: 19 additions & 12 deletions morebuiltins/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,23 @@ def xor_encode_decode(data, key):
return bytes([b ^ k for b, k in zip(data, extended_key)])


def is_running_win32(pid: int):
with os.popen('tasklist /fo csv /fi "pid eq %s"' % int(pid)) as f:
f.readline()
text = f.readline()
return bool(text)


def is_running_linux(pid: int):
try:
os.kill(int(pid), 0)
return True
except OSError:
return False
except SystemError:
return True


def is_running(pid):
"""Check if the given process ID is running.
Expand All @@ -910,19 +927,9 @@ def is_running(pid):
except ValueError:
return False
if sys.platform == "win32":
with os.popen('tasklist /fo csv /fi "pid eq %s"' % int(pid)) as f:
f.readline()
text = f.readline()
return bool(text)
return is_running_win32(pid)
else:
try:
os.kill(int(pid), 0)
return True
except OSError:
return False
except SystemError:
# maybe windows?
return True
return is_running_linux(pid)


def set_pid_file(
Expand Down

0 comments on commit 25419de

Please sign in to comment.