Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #931 by using psutil to terminate #932

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion py4web/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from contextlib import redirect_stderr, redirect_stdout

import portalocker
import psutil
from watchgod import awatch

# Optional web servers for speed
Expand Down Expand Up @@ -1710,7 +1711,15 @@ def start_server(kwargs):

# Catch interrupts like Ctrl-C if needed
def kill_all(sig, _):
os.kill(os.getpid(), signal.SIGKILL)
# better way to kill, adapted from: https://stackoverflow.com/a/70565806
main_process = psutil.Process()
children = main_process.children(recursive=True)
for child in children:
child.terminate() # friendly termination (SIGTERM)
_, still_alive = psutil.wait_procs(children, timeout=2)
for child in still_alive:
child.kill() # unfriendly termination (SIGKILL)
main_process.kill() # SIGKILL

signal.signal(signal.SIGINT, kill_all)

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rocket3 >= 20241019.1
yatl >= 20230507.3
pydal >= 20241027.1
watchgod >= 0.6
psutil

# optional modules:
# gunicorn
Expand Down
Loading