Cherrypy multithreading example
Asked Answered
S

1

6

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.

Stomodaeum answered 2/7, 2010 at 9:31 Comment(1)
You should try with a specific software, e.g. httperf: httperf --server=127.0.0.1 --port=6060 --num-conn=50 --rate=10Garb
L
2

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.

Layton answered 2/7, 2010 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.