Pra*_*lad 6 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>'
Run Code Online (Sandbox Code Playgroud)
现在假设我在浏览器的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)>
Run Code Online (Sandbox Code Playgroud)
我可以清楚地理解它正在创建处理每个新请求的新线程,但我无法弄清楚为什么每次我
开始...结束......开始......结束
以及为什么不开始...开始......结束..有时候结束
因为我的假设是time.sleep会使一些线程暂停而其他人可以在那时执行.
这几乎可以肯定是您的浏览器的限制,而不是 CherryPy 的限制。例如,Firefox 2 不会向同一域发出超过 2 个并发请求,即使有多个选项卡也是如此。如果每个选项卡也获取一个网站图标...那么您的处理程序一次只会点击一个。
请参阅http://www.cherrypy.org/ticket/550获取具有类似源代码和更长证明的票证。