Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cherrypy multithreading example

I do know that cherrypy is a multithreaded and also has a threadpool implementation.
So I wanted to try an example showing multithreaded behaviour.
Now lets say I've my some function in the root class and rest all things are configured

def testPage(self, *args, **kwargs):
    current = threading.currentThread()
    print 'Starting ' , current
    time.sleep(5)
    print 'Ending ' ,current
    return '<html>Hello World</html>'

Now lets say I run my page as http://localhost:6060/root/testPage in 3-4 tabs of browser.
What result I get is

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)>

The thing I can clearly understand that it's creating new threads for processing every new request but I cannot figure out why every time I get
starting...ending..starting..ending
and why not starting...starting..ending..ending sometimes
Because what my assumption is that time.sleep will make some thread to suspend and other one can execute at that time.

like image 877
Prahlad Avatar asked Jul 02 '10 09:07

Prahlad


1 Answers

This is almost certainly a limitation of your browser and not of CherryPy. Firefox 2, for example, will make no more than 2 concurrent requests to the same domain, even with multiple tabs. And if each tab is also fetching a favicon...that leaves one hit at a time on your handler.

See http://www.cherrypy.org/ticket/550 for a ticket with similar source code, and a longer proof.

like image 160
fumanchu Avatar answered Oct 05 '22 11:10

fumanchu