我知道cherrypy是一个多线程的,也有一个线程池实现。
因此,我想尝试一个显示多线程行为的示例。
现在假设我在根类中有一些函数,其余的都配置好了
def testPage(self, *args, **kwargs):
current = threading.currentThread()
print 'Starting ' , current
time.sleep(5)
print 'Ending ' ,current
return '<html>Hello World</html>'现在假设我在浏览器的3-4个选项卡中以http://localhost:6060/root/testPage的形式运行我的页面。
我得到的结果是
Starting <WorkerThread(CP WSGIServer Thread-10, started 4844)>
Ending <WorkerThread(CP WSGIServer Thread-10, started 4844)>
Starting <WorkerThread(CP WSGIServer Thread-7, started 4841)>
Ending <WorkerThread(CP WSGIServer Thread-7, started 4841)>
Starting <WorkerThread(CP WSGIServer Thread-10, started 4844)>
Ending <WorkerThread(CP WSGIServer Thread-10, started 4844)>我可以清楚地理解它正在创建新的线程来处理每个新的请求,但是我不能理解为什么每次我得到
starting...ending..starting..ending
为什么有时不使用starting...starting..ending..ending?
因为我的假设是,time.sleep会让一些线程挂起,而另一个线程可以在那个时候执行。
发布于 2010-07-02 23:47:18
这几乎可以肯定是浏览器的限制,而不是CherryPy的限制。例如,即使有多个选项卡,Firefox2对同一个域的并发请求也不会超过2个。如果每个选项卡都在获取一个favicon...that,那么在处理程序上一次只留下一个命中。
请参阅http://www.cherrypy.org/ticket/550获取具有类似源代码的票据,以及更长的证明。
https://stackoverflow.com/questions/3164560
复制相似问题