Cherrypy多线程示例

时间:2010-07-02 09:31:22

标签: python multithreading cherrypy

我知道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)>

我可以清楚地理解它正在创建处理每个新请求的新线程,但我无法弄清楚为什么每次我开始...结束......开始......结束 为什么不开始...开始..结束...有时候结束 因为我的假设是time.sleep会使一些线程暂停而其他人可以在那时执行。

1 个答案:

答案 0 :(得分:2)

这几乎肯定是您浏览器的限制,而不是CherryPy。例如,Firefox 2对同一个域的并发请求不会超过2个,即使有多个选项卡也是如此。并且如果每个选项卡也提取了一个图标...在处理程序上一次只有一个点击。

有关具有类似源代码和更长证明的故障单,请参阅http://www.cherrypy.org/ticket/550