Use asynchronous Socket programming performance testing in Python
- 2020-04-02 13:45:40
- OfStack
OK, first write a python socket server, to open three port: 10000100, 01100, 02. Krondo example is a port, each server binding tests need to open three shell respectively, were running. This is too much trouble, it respectively with three Thread to run these services.
import optparse
import os
import socket
import time
from threading import Thread
import StringIO
txt = '''1111
2222
3333
4444
'''
def server(listen_socket):
while True:
buf = StringIO.StringIO(txt)
sock, addr = listen_socket.accept()
print 'Somebody at %s wants poetry!' % (addr,)
while True:
try:
line = buf.readline().strip()
if not line:
sock.close()
break
sock.sendall(line) # this is a blocking call
print 'send bytes to client:%s' % line
#sock.close()
except socket.error:
sock.close()
break
time.sleep(1) #server and client After the connection, server It deliberately waits a second for each word to be sent before sending another word
def main():
ports = [10000, 10001, 10002]
for port in ports:
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
addres = (str('127.0.0.1'), port)
listen_socket.bind(addres)
listen_socket.listen(5)
print "start listen at:%s" % (port,)
worker = Thread(target = server, args = [listen_socket])
worker.setDaemon(True)
worker.start()
if __name__ == '__main__':
main()
while True:
time.sleep(0.1) # If you don't sleep if ,CPU Will be Python Completely occupied
pass
The following is a client, which USES the asynchronous network to connect to the three-port server:
import socket
if __name__ == '__main__':
ports = [10000, 10001, 10002]
for port in ports:
address = (str('127.0.0.1'), port)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(address)
poem = ''
while True:
data = sock.recv(4)
if not data:
sock.close()
break
poem += data
print poem
The following asynchronous client is used to read, the code is as follows:
import datetime, errno, optparse, select, socket
def connect(port):
"""Connect to the given server and return a non-blocking socket."""
address = (str('127.0.0.1'), port)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(address)
sock.setblocking(0)
return sock
def format_address(address):
host, port = address
return '%s:%s' % (host or '127.0.0.1', port)
if __name__ == '__main__':
ports = [10000, 10001, 10002]
start = datetime.datetime.now()
sockets = map(connect, ports)
poems = dict.fromkeys(sockets, '') # socket -> accumulated poem
# socket -> task numbers
sock2task = dict([(s, i + 1) for i, s in enumerate(sockets)])
sockets = list(sockets) # make a copy
while sockets:
# using select To ensure that those are readable asynchronous socket You can start reading immediately IO
#OS Non-stop searching is currently available read the socket , return if you have rlist
rlist, _, _ = select.select(sockets, [], [])
for sock in rlist:
data = ''
while True:
try:
new_data = sock.recv(1024)
except socket.error, e:
if e.args[0] == errno.EWOULDBLOCK:
break
raise
else:
if not new_data:
break
else:
print new_data
data += new_data
task_num = sock2task[sock]
if not data:
sockets.remove(sock)
sock.close()
print 'Task %d finished' % task_num
else:
addr_fmt = format_address(sock.getpeername())
msg = 'Task %d: got %d bytes of poetry from %s'
print msg % (task_num, len(data), addr_fmt)
poems[sock] += data
elapsed = datetime.datetime.now() - start
print 'Got poems in %s' % elapsed
The result is a read in just 4 seconds. Three times more efficient than just synchronizing sockets. There are two main points for asynchronous transformation of client:
In synchronous mode, the client creates sockets respectively. In asynchronous mode, the client creates all the sockets in the first place.
Set socket to asynchronous mode by "sock. Setblocking (0)".
Returns readable IO through the select pair of Unix systems
The core is lines 26 and 29. In particular, the select operation on line 29 returns a list of sockets to be read.