简体   繁体   中英

Creating multithread server in python

I am newbie for implementing multithread web server in python. I searched the internet and found some resources to make it.After that I tested in apachebench via this command :

ab -n 1000 -c 5 localhost:8888/

Results for this command :

Concurrency Level:      5
Time taken for tests:   1.692 seconds
Complete requests:      1000
Failed requests:        0
Requests per second:    591.16 [#/sec] (mean)
Time per request:       8.458 [ms] (mean)
Time per request:       1.692 [ms] (mean, across all concurrent requests)

ab -n 1000 -c 100 localhost:8888/

Results :

Concurrency Level:      100
Time taken for tests:   1.699 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      12078000 bytes
HTML transferred:       12000000 bytes
Requests per second:    588.50 [#/sec] (mean)
Time per request:       169.923 [ms] (mean)
Time per request:       1.699 [ms] (mean, across all concurrent requests)
Transfer rate:          6941.33 [Kbytes/sec] received

Now , My question is that Why these values(time taken for tests and request per second ..) is not change noticeable ? Can anyone interpret this test results ?

Code:

import thread
import socket
import sys

def threadFunction(client_connection,client_address):
    print 'Client thread created..'
    request = client_connection.recv(1024)
    print request
    if request=="":
    sys.exit()
    client_connection.send("HTTP/1.1 400 bad request\nContent-Type: text/html; charset=UTF-8\nContent-Lenght: "+b)
    client_connection.close()

HOST, PORT = '', 8888

listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(100)


while True:
    print 'Serving HTTP on port %s ...' % PORT
    client_connection, client_address = listen_socket.accept()
    print 'client_connection: %s , client_address : %s' % (client_connection,client_address)
    try:
                thread.start_new_thread( threadFunction, (client_connection,client_address) ) 
    except IOError:
        print "Failed in initialize thread.."
listen_socket.close() 

Actually Apache is a single threaded benchmark tool and Time taken for tests: is the time taken by the 'ab' to start test ..

Concurrency versus Threads

Note that ApacheBench will only use one operating system thread regardless of the concurrency level (specified by the -c parameter). In some cases, especially when benchmarking high-capacity servers, a single instance of ApacheBench can itself be a bottleneck. When using ApacheBench on hardware with multiple processor cores, additional instances of ApacheBench may be used in parallel to more fully saturate the target URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM