-
Notifications
You must be signed in to change notification settings - Fork 692
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2626 from xrmx/methane-worker-graceful-stop
core/uwsgi: graceful stop worker when max_requests/reload_on_*
- Loading branch information
Showing
4 changed files
with
90 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# https://github.com/unbit/uwsgi/pull/2615 | ||
# atexit should be called when reached max-requests. | ||
# | ||
# Start this app: | ||
# | ||
# $ ./uwsgi --http-socket :8000 --master -L --wsgi-file=tests/threads_atexit.py \ | ||
# --workers 1 --threads 32 --max-requests 40 --min-worker-lifetime 6 --lazy-apps | ||
# | ||
# Access to this app with hey[1]: | ||
# | ||
# # Do http access for 5 minutes with 32 concurrency | ||
# $ ./hey -c 32 -z 5m 'http://127.0.0.1:8000/' | ||
# | ||
# Search how many stamp files: | ||
# | ||
# $ ls uwsgi_worker*.txt | wc -l | ||
# 39 # should be 0 | ||
# | ||
# [1] https://github.com/rakyll/hey | ||
|
||
import atexit | ||
import os | ||
import sys | ||
import time | ||
|
||
|
||
pid = os.getpid() | ||
stamp_file = f"./uwsgi_worker{pid}.txt" | ||
|
||
|
||
with open(stamp_file, "w") as f: | ||
print(time.time(), file=f) | ||
|
||
|
||
@atexit.register | ||
def on_finish_worker(): | ||
print(f"removing {stamp_file}", file=sys.stderr) | ||
os.remove(stamp_file) | ||
|
||
|
||
def application(env, start_response): | ||
time.sleep(1) | ||
start_response('200 OK', [('Content-Type', 'text/html')]) | ||
return [b"Hello World"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# https://github.com/unbit/uwsgi/pull/2615 | ||
# CPU heavy application in multi threaded uWSGI doesn't shutdown gracefully. | ||
# | ||
# $ ./uwsgi \ | ||
# --wsgi-file=threads_heavy.py --master --http-socket=:8000 \ | ||
# --workers=4 --threads=8 --max-requests=20 --min-worker-lifetime=6 -L \ | ||
# --worker-reload-mercy=20 2>&1 | tee uwsgi.log | ||
# | ||
# $ hey -c 16 -z 3m 'http://127.0.0.1:8000/' | ||
# | ||
# $ grep MERCY uwsgi.log | ||
# Tue Mar 19 14:01:59 2024 - worker 1 (pid: 62113) is taking too much time to die...NO MERCY !!! | ||
# Tue Mar 19 14:02:23 2024 - worker 2 (pid: 62218) is taking too much time to die...NO MERCY !!! | ||
# ... | ||
# | ||
# This was caused by pthread_cancel() is called from non-main thread. | ||
|
||
def fibonacci(n): | ||
if n <= 1: | ||
return n | ||
return fibonacci(n - 1) + fibonacci(n - 2) | ||
|
||
|
||
def application(env, start_response): | ||
start_response('200 OK', [('Content-Type', 'text/html')]) | ||
n = 24 | ||
r = fibonacci(n) | ||
s = f"F({n}) = {r}" | ||
return [s.encode()] |