Skip to content

Commit 00656e1

Browse files
committed
Refactor OS-call to stop the current process
1 parent bbd80e8 commit 00656e1

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

kasatk/__main__.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@
1818
logger = logging.getLogger(__name__)
1919

2020

21+
def apoptosis():
22+
"""Tell the operating system to kill us. This is useful in case we cannot
23+
die. For example, a child thread might refuse to die.
24+
"""
25+
import os
26+
27+
# Windows option
28+
if os.name == "nt":
29+
import subprocess
30+
31+
subprocess.run(["taskkill", "/IM", str(os.getpid()), "/F"])
32+
# *Nix option
33+
else:
34+
os.kill(os.getpid(), signal.SIGKILL)
35+
36+
2137
def resource_path(relative_path):
2238
"""Find a resource in a PyInstaller executable, or in the local directory"""
2339
if hasattr(sys, "_MEIPASS"):
@@ -407,11 +423,9 @@ def main():
407423
# asyncio task that is hogging the CPU and not surrending control
408424
# via await.
409425
logger.warning("Failed to stop event thread.")
410-
# Since asyncio is not behaving, we need to kill ourselves.
411-
if os.name == "nt":
412-
subprocess.run(["taskkill", "/IM", str(os.getpid()), "/F"])
413-
else:
414-
os.kill(os.getpid(), signal.SIGKILL)
426+
# Since asyncio is not behaving, we need the operating system to
427+
# end this process
428+
apoptosis()
415429

416430

417431
if __name__ == "__main__":

0 commit comments

Comments
 (0)