[New-bugs-announce] [issue14073] allow per-thread atexit()

Tarek Ziadé report at bugs.python.org
Tue Feb 21 15:12:08 CET 2012

New submission from Tarek Ziadé <ziade.tarek at gmail.com>:

If you try to run the code below and stop it with ctrl+C, it will lock because atexit is never reached.

Antoine proposed to add a way to have one atexit() per thread, so we can call some cleanup code when the app shuts down and there are running threads.

from wsgiref.simple_server import make_server
import threading
import time
import atexit

class Work(threading.Thread):
    def __init__(self):
        self.running = False

    def run(self):
        self.running = True
        while self.running:

    def stop(self):
        self.running = False

worker = Work()

def shutdown():
    # bye-bye
    print 'bye bye'


def hello_world_app(environ, start_response):
    status = '200 OK' # HTTP Status
    headers = [('Content-type', 'text/plain')]
    start_response(status, headers)
    return ["Hello World"]

def main():
    return make_server('', 8000, hello_world_app)

if __name__ == '__main__':
    server = main()

messages: 153871
nosy: tarek
priority: normal
severity: normal
status: open
title: allow per-thread atexit()
type: behavior
versions: Python 3.2, Python 3.3, Python 3.4

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list