十二月 11, 2008
» [tips] 針對特定model改變django ORM預設delete()行為的方法

Django ORM裡的預設delete行為是去模擬ON DELETE CASCADE, 主要是為了保證資料的一致性, 但是雖然在自帶的admin介面裡會提示是否要刪除其他相關聯的資料, 不過仍然可能會對某些需求帶來一些困擾, 而且到目前為止並沒有一個標準的方法來更改這個機制, 這邊我提供一個簡單的方法將model的ON DELETE CASCADE行為改為RESTRICT, 就是在需要不同機制的model裡去override orm原本的delete行為 :

 
def delete(self):
s = CollectedObjects()
self._collect_sub_objects(s)
if len(s.items()) == 1:
super(self.__class__, self).delete()
else:
pass



這可確保當沒有任何關聯物件時才會刪除, 否則只會安靜的pass,
this hack probably need django 1.0+.

十月 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.

六月 24, 2007
» Thread dying problem may fixed in django/flup.

一直都耳聞django的apache+mod_fastcgi+thread mode在高負載狀況下有問題,
甚至也有人將之歸結於python的thread實作有問題,
所以我一直以來都是使用prefork的方式來啟動fastcgi/wsgi,
上禮拜在替django作負載調試的時候,
也發現thread mode在超高壓力測試的情況下,的確會停止運作造成service unavailable
(concurrency > 1000)
因此才花時間找出真正的原因(至少在我這邊的原因),
應該是django所採用(其實不止django, pylons跟turbogears也有採用)
的FastCGI to WSGI adapter -- flup,
在threadpool的實作上有些許的小問題, 沒有對運作異常的thread拋出例外的情形加以處理,
才導致了發生問題的thread沒有再次進入threadpool,所以運作一陣子後會讓所有thread悄悄死去.

在修正了這個問題之後, 即使在concurrency > 1000的情況下仍然保持順暢運作,
也擺脫了使用modpython或prefork mode的fastcgi大量耗用伺服器記憶體的情形,
thread mode + FastCGI/WSGI 果然才是王道!

已提交patch, 期待新版的flup收納之後, 讓python的web framework在thread mode下運作更順利.

六月 10, 2007
» [link] 有愛連結: 以 New Gameday 數據解析王建民的投球特性

剛剛讀到這篇 Whither所寫的 漫步在綠海豚街上: 以 New Gameday 數據解析王建民的投球特性, 看到這篇文章用心的透過Gameday的XML數據分析王建民的投球. 着實又讓大家見識到所謂的"有愛"是怎麼回事 :D

MLB於2006年裝設了新版的gameday,除了將線上轉播帶進了一個新的時代,在棒球場上更裝設了30fps的高速攝影機以便能及時的分析到投打對決的實況, 然而目前只有九隊安裝高速攝影裝備, 很可惜洋基目前尚未裝設, 所以大大的減少了分析王建民投球數據的資料, 而該文便是在分析王建民於2007/05/16芝加哥白襪隊客場先發時所留下的XML數據. 內文有詳實的內容跟美觀的圖表, 透過科學化的角度分析投球內容 ,非常值得所有洋基/王建民的球迷一讀.

五月 31, 2007
» Google gears !

今天進 google reader的時候發現右上角多了幾個字: offline

好奇的按下去之後發現 google reader已經引進了
google的離線儲存新技術: google gears.

也就是說安裝之後,
google reader瞬間變成離線也能閱讀的RSS瀏覽器了 !
看來離線的gmail也不遠了...

以技術面來說google gears
以new bsd license放出,是完完全全的自由軟體.
支援Windows/Mac/Linux ,
並且支援IE/Firefox/Safari(未來將會支援,目前在mac上支援firefox)
等於所有主流系統全部支援.

google gears除了離線存取現有的web application之外,
還提供了一個WorkerPool的API幫助programmer將資源吃重的一部份程式以非同步方式存取,
並且還內建輕量級資料庫sqlite 支援使用javascript撰寫sql
(沒錯... 就是像下面這樣
var rs = db.execute('SELECT dish FROM recipe WHERE recipe MATCH ?', ['tomatoes']);
)
之前雖然也有如dojo storage之類的解決方案.但總不如早已習慣的rdbms來的方便. google幹的好啊!

google gears目前雖然仍是beta中,
但解決了web application離線存取這個麻煩的問題,
由於是完完全全的自由軟體(允許商業使用)
在未來有機會成為業界的新標準.

更多詳細的資訊請參考:

http://code.google.com/apis/gears/index.html
http://gears.google.com/

五月 5, 2007
» [link] 誰說只有wiki可以這樣搞.....orz

pure html+js ... 越來越有趣了 :P

Terminal

Home

Blog

biggo.com.tw

A Django site.