From 9cacf9c489c5b77d920f33c3c616581b26a7e2c4 Mon Sep 17 00:00:00 2001 From: ianb Date: Wed, 31 Jan 2007 20:01:19 +0000 Subject: rewrite watchthreads to use the new paste.util.template --- paste/debug/watchthreads.py | 145 ++++++++++++++++++++++++-------------------- 1 file changed, 80 insertions(+), 65 deletions(-) (limited to 'paste') diff --git a/paste/debug/watchthreads.py b/paste/debug/watchthreads.py index 93523e3..62636b7 100644 --- a/paste/debug/watchthreads.py +++ b/paste/debug/watchthreads.py @@ -2,13 +2,14 @@ Watches the key ``paste.httpserver.thread_pool`` to see how many threads there are and report on any wedged threads. """ -import string import cgi import time +from thread import get_ident from paste import httpexceptions from paste.request import construct_url, parse_formvars +from paste.util.template import HTMLTemplate, bunch -page_template = string.Template(''' +page_template = HTMLTemplate(''' - $title + {{title}} -

$title

- $kill_message -
Pool size: $nworkers - ($nworkers_used used including current request)
- $body - - -''') +

{{title}}

+ {{if kill_thread_id}} +
+ Thread {{kill_thread_id}} killed +
+ {{endif}} +
Pool size: {{nworkers}} + ({{nworkers_used}} used including current request)
+ +{{for thread in threads}} -thread_template = string.Template(''' - - - + + + - + - + -
Thread$thread_id $kill
+ Thread + {{if thread.thread_id == this_thread_id}} + (this request) + {{endif}} + {{thread.thread_id}} + {{if allow_kill}} +
+ + +
+ {{endif}} +
+
Time processing request$time_html{{thread.time_html|html}}
URI$uri_short{{if thread.uri == 'unknown'}} + unknown + {{else}}{{thread.uri_short}} + {{endif}} +
+ - $environ_rows + {{for loop, item in looper(sorted(thread.environ.items()))}} + {{py:key, value=item}} + + + + + {{endfor}}
{{key}}{{value}}
-''') -environ_template = string.Template(''' - - $key - $value - -''') - -kill_template = string.Template(''' -
- - -
-''') - -kill_message_template = string.Template(''' -
- Thread $thread_id killed -
-''') +{{endfor}} + + + +''', name='watchthreads.page_template') class WatchThreads(object): @@ -128,44 +148,39 @@ class WatchThreads(object): start_response('200 OK', [('Content-type', 'text/html')]) form = parse_formvars(environ) if form.get('kill'): - kill_message = kill_message_template.substitute( - thread_id=form['kill']) + kill_thread_id = form['kill'] else: - kill_message = '' + kill_thread_id = None thread_pool = environ['paste.httpserver.thread_pool'] nworkers = thread_pool.nworkers now = time.time() + workers = thread_pool.worker_tracker.items() workers.sort(key=lambda v: v[1][0]) - body = [] + threads = [] for thread_id, (time_started, worker_environ) in workers: + thread = bunch() + threads.append(thread) if worker_environ: - uri = construct_url(worker_environ) - else: - uri = 'unknown' - if self.allow_kill: - kill = kill_template.substitute( - script_name = environ['SCRIPT_NAME'], - thread_id=thread_id) + thread.uri = construct_url(worker_environ) else: - kill = '' - thread = thread_template.substitute( - thread_id=thread_id, - time_html=format_time(now-time_started), - uri=uri, - uri_short=shorten(uri), - environ_rows=format_environ(worker_environ), - kill=kill, - ) - body.append(thread) - + thread.uri = 'unknown' + thread.thread_id = thread_id + thread.time_html = format_time(now-time_started) + thread.uri_short = shorten(thread.uri) + thread.environ = worker_environ + page = page_template.substitute( title="Thread Pool Worker Tracker", - body=''.join(body), nworkers=nworkers, nworkers_used=len(workers), - kill_message=kill_message) + script_name=environ['SCRIPT_NAME'], + kill_thread_id=kill_thread_id, + allow_kill=self.allow_kill, + threads=threads, + this_thread_id=get_ident()) + return [page] def kill(self, environ, start_response): -- cgit v1.2.1