File: threads_atexit.py

package info (click to toggle)
uwsgi 2.0.31-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,564 kB
  • sloc: ansic: 87,066; python: 7,004; cpp: 1,133; java: 708; perl: 678; sh: 585; ruby: 555; makefile: 148; xml: 130; cs: 121; objc: 37; php: 28; erlang: 20; javascript: 11
file content (44 lines) | stat: -rw-r--r-- 1,005 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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"]