X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=python%2Fashd%2Fwsgidir.py;h=335a5d797e6664b52832ec9b6f8836fcea9499fa;hb=14ef1e0ed9e18ea341f21287fe9ca722985dfeee;hp=037544d959c5fabf50e9c4895dd83fd5504776b6;hpb=c06db49a3a4bfbf14b1661b667e1ed1cbab2bcd0;p=ashd.git diff --git a/python/ashd/wsgidir.py b/python/ashd/wsgidir.py index 037544d..335a5d7 100644 --- a/python/ashd/wsgidir.py +++ b/python/ashd/wsgidir.py @@ -1,6 +1,12 @@ import os, threading, types import wsgiutil +class cachedmod: + def __init__(self, mod, mtime): + self.lock = threading.Lock() + self.mod = mod + self.mtime = mtime + exts = {} modcache = {} cachelock = threading.Lock() @@ -19,9 +25,10 @@ def getmod(path): cachelock.acquire() try: if path in modcache: - mod, mtime = modcache[path] - if sb.st_mtime <= mtime: - return mod + entry = modcache[path] + if sb.st_mtime <= entry.mtime: + return entry + f = open(path) try: text = f.read() @@ -31,31 +38,44 @@ def getmod(path): mod = types.ModuleType(mangle(path)) mod.__file__ = path exec code in mod.__dict__ - modcache[path] = mod, sb.st_mtime - return mod + entry = cachedmod(mod, sb.st_mtime) + modcache[path] = entry + return entry finally: cachelock.release() def chain(path, env, startreq): mod = getmod(path) - if hasattr(mod, "wmain"): - return (mod.wmain())(env, startreq) - elif hasattr(mod, "application"): - return mod.application(env, startreq) - return wsgi.simpleerror(env, startreq, 500, "Internal Error", "Invalid WSGI handler.") + entry = None + if mod is not None: + mod.lock.acquire() + try: + if hasattr(mod, "entry"): + entry = mod.entry + else: + if hasattr(mod.mod, "wmain"): + entry = mod.mod.wmain([]) + elif hasattr(mod.mod, "application"): + entry = mod.mod.application + mod.entry = entry + finally: + mod.lock.release() + if entry is not None: + return entry(env, startreq) + return wsgiutil.simpleerror(env, startreq, 500, "Internal Error", "Invalid WSGI handler.") exts["wsgi"] = chain def application(env, startreq): if not "SCRIPT_FILENAME" in env: - return wsgiutil.simplerror(env, startreq, 500, "Internal Error", "The server is erroneously configured.") + return wsgiutil.simpleerror(env, startreq, 500, "Internal Error", "The server is erroneously configured.") path = env["SCRIPT_FILENAME"] base = os.path.basename(path) p = base.rfind('.') if p < 0 or not os.access(path, os.R_OK): - return wsgiutil.simplerror(env, startreq, 500, "Internal Error", "The server is erroneously configured.") + return wsgiutil.simpleerror(env, startreq, 500, "Internal Error", "The server is erroneously configured.") ext = base[p + 1:] if not ext in exts: - return wsgiutil.simplerror(env, startreq, 500, "Internal Error", "The server is erroneously configured.") + return wsgiutil.simpleerror(env, startreq, 500, "Internal Error", "The server is erroneously configured.") return(exts[ext](path, env, startreq)) def wmain(argv):