2010-07-02 18 views
6

So che cherrypy è un multithreading e ha anche un'implementazione threadpool.
Quindi volevo provare un esempio che mostra il comportamento multithread.
Ora lascia dire che ho la mia qualche funzione nella classe principale e resto tutte le cose sono configurati
Esempio di multithreading Cherrypy

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

Ora lascia dire corro la mia pagina come http://localhost:6060/root/testPage in 3-4 schede del browser.
Che risultato che ottiene è

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

La cosa che posso capire chiaramente che si tratta di creare nuove discussioni per l'elaborazione di ogni nuova richiesta ma non riesco a capire il motivo per cui ogni volta che ricevo
di partenza ... ending..starting ..in via
e perché non iniziare ... a partire da..inviare..in volte a volte
Perché la mia ipotesi è che time.sleep farà in modo che alcuni thread vengano sospesi e altri possano essere eseguiti in quel momento.

+0

Dovresti provare con un software specifico, ad es. httperf: httperf --server = 127.0.0.1 --port = 6060 --num-conn = 50 --rate = 10 –

risposta

2

Questo è quasi certamente un limite del browser e non di CherryPy. Ad esempio, Firefox 2 non eseguirà più di 2 richieste simultanee allo stesso dominio, anche con più schede. E se ogni scheda recupera anche una favicon ... che lascia un colpo alla volta sul tuo gestore.

Vedere http://www.cherrypy.org/ticket/550 per un ticket con codice sorgente simile e una prova più lunga.