十月 30, 2007
» [tips] using flup as trac's default fastcgi backend. (with lighttpd)

I've found trac's fastcgi backend simply out-dated. I thinks it's direct copied from an old flup source, and since then flup really updates a lot, I've already use thread pool techniques everywhere in our web applications/frameworks for a while, so current trac's fastcgi backend implement just so out-dated and looks wierd when I installed it, really.

But thanks there's WSGI to save us ! I could just replace the fastcgi backend to flup-0.5 (latest, and author claims it maybe final version of flup) and SAVE MEMORY !

and it's simple too,
just install the flup package, and edit your old trac.fcgi / lighttpd's settings.

trac.fcgi:


try:
import pkg_resources

from trac import __version__ as VERSION
from trac.web.main import dispatch_request

from flup.server.fcgi import WSGIServer
WSGIServer(dispatch_request,maxSpare=100,minSpare=100,maxThreads=100).run()

except SystemExit:
raise
except Exception, e:
print 'Content-Type: text/plain\r\n\r\n',
print 'Oops...'
print
print 'Trac detected an internal error:'
print
print e
print
import traceback
import StringIO
tb = StringIO.StringIO()
traceback.print_exc(file=tb)
print tb.getvalue()


you can change the line which specify
maxSpare,minSpare,maxThreads,I use 100,
you should use less than 381 if you're using
python 2.4 (you'll hit stack limit at least on i386-linux) however if you're using python 2.5 you can change stack size via thread.stack_size,
though I don't think anyone would need that high concurrency (who knows?!).

below's also a sample setting in lighttpd.conf,

what really matters here is to remember add a
"max-procs" => 1
line in it, since we've already using thread pools technique, forking another process seems redundant (and WASTE memory).


fastcgi.server += ("/sandbox" =>
("sandbox" =>
("socket" => "/tmp/trac-sandbox.sock", #need to change for each
"bin-path" => "/var/trac/cgi-bin/trac.fcgi",
"max-procs" => 1, #default 4
"check-local" => "disable",
"bin-environment" =>
("TRAC_ENV" => "/var/trac/sandbox")
)
)
)


oh, btw I'm using trac-0.11dev, but I think any version below 0.11 which implement WSGI should also works.

七月 3, 2007
» 治療系的修改

把本站的 fastcgi driving script 改成:

#!/your/path/to/python

import sys, os
from datetime import datetime
import traceback

try:
    sys.path.insert(0,'/your/path/to/python/package')
    os.environ['DJANGO_SETTINGS_MODULE'] = 'yoursite.settings'
    from django.core.servers.fastcgi import runfastcgi
    runfastcgi(method="prefork", daemonize="false",
            maxrequests=100,
            maxspare=2, minspare=1, maxchildren=5)
except Exception, e:
    logpath = "/your/path/to/traceback.log"
    if os.path.isfile(logpath):
        f = open(logpath, 'a')
    else:
        f = open(logpath, 'w')
    f.write("logged at %s :\n"%datetime.now())
    traceback.print_exc(file=f)
    f.write("log end.\n\n")
    f.close()

# vim: set ft=python ai et nu sw=4 ts=4 tw=79:

看看會不會也有幫助。如果 maxrequests=100 有用的話,crontab 就可以丟掉了。

» 沒事就死

*/20 * * * * killall bad.fcgi

10 隻小孩還嫌少?只給你二十分鐘好活!

近來訪客可能經常看到本站的 500 錯誤畫面,這都是我寫的程式不乖。本人已經縮短 application server 的生命,如果還看到 500,還請向本人舉報。謝謝大家。

biggo.com.tw

A Django site.