web.py sucks

web.py might be famous, but it really sucks in performance.

I tried code.py from tutorial and was shocked. The `ab -c 10 -n 1000` benchmark shows code.py can only handle 200 request per second. (CherryPy is about 600 r/s). And in order to boost performance, I also tried mod_wsgi with web.py, the perfomance gain in 20% ~ 30%.

The API design of web.py is not beautiful and even ugly, how stupid it maps URL into a class but not a callable object and force people to write GET, POST functions. It’s not the OO way in which CherryPy code looks much better.

Finally, my suggestion is to get rid of web.py as soon as possible, even in small project. Try mod_wsgi instead if in hurry.

BTW, mod_wsgi is quite excellent, much better than mod_python.

Notes on Python Socket Module

I was surprised to see how Python implements socket module in its C level today. Python doesn’t use real connect(2), it implements internal_connect via poll(2). I think it caused by GIL, you can find lots of Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS macros around the code to ensure the code runs correctly in multi-threads mode.

How to terminate Thread in Python?

The safest way is using threading.Event to avoid sync problem.

import threading
 
class TestThread(threading.Thread):
 
    def __init__(self):
        self.stopEvent = threading.Event()
 
    def run(self):
        while not self.stopEvent.isSet():
            pass
 
    def stop(self):
        self.stopEvent.set()

玩了下 syslog

写了一个日志模块,Java 的经验告诉我直接用 logging 模块要比自己依赖特定的 logger 没快要好。不过这回碰到 syslog,我就错了。

Python 的标准 logging 模块的确不如直接用 syslog 模块好使。syslog 的接口很简单,后面的工作就交给自己去配置 syslog.conf 了,相当的好用,想存哪存哪,还能自己配置过滤不同级别的日志。

PS:朱同学说 BSD 下的 syslog 相当优秀,Linux 下的就是一托屎了(据说记一下日志需要 25ms :O),BSD 下那就是快的你根本没什么感觉。