[python3.3]Python非同步Socket程式設計【TCP】

joeblackzqq發表於2014-06-27

參考: http://www.cnblogs.com/snailrun/p/3805188.html

非同步網路據說能極大的提高網路server的連線速度,所以打算寫一個專題,來學習和了解非同步網路.因為Python有個非常出名的非同步Lib:Twisted,所以就用Python來完成. 

OK,首先寫一個pythone socket的server段,對開放三個埠:10000,10001,10002.krondo的例子中是每個server繫結一個埠,測試的時候需要分別開3個shell,分別執行.這太麻煩了,就分別用三個Thread來執行這些services. 

import optparse
import os
import socket
import time
from threading import Thread
from io import StringIO

txt = '''1111
2222
3333
4444
'''

# 服務端程式處理過程
def server(listen_socket):
	while True:
		buf = 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.encode('utf8')) # this is a blocking call
				print('send bytes to client: %s' %line)
			except socket.error:
				sock.close()
				break
			time.sleep(0.5) # server每傳送一個單詞後等待一會
		sock.close()
		print('\n')

# 同時開啟三個服務端執行緒,分別在三個埠監聽
# 服務端程式為阻塞方式,只能一次服務於一個客戶端
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 = ('127.0.0.1', port)
		listen_socket.bind(addres)
		listen_socket.listen(5)
		print("start listen at: %d" %port)
		worker = Thread(target = server, args = [listen_socket])
		worker.setDaemon(True)
		worker.start()

if __name__ == '__main__':
	main()
	while True:
		time.sleep(0.1) # 如果不sleep的話, CPU會被Python完全佔用了

下面是一個client, 用阻塞方式,先後連線這個三個埠的server: 

from socket import *

# 建立三個客戶端,分別連線三個不同的服務端程式, 接收服務端傳過來的資料並列印
# 這三個客戶端是阻塞方式通訊的
if __name__ == '__main__':
	ports = [10000, 10001, 10002]
	for port in ports:
		address = ('127.0.0.1', port)
		sock = socket(AF_INET, SOCK_STREAM)
		sock.connect(address)
		poem = ''
		while True:
			data = sock.recv(4)
			if not data:
				sock.close()
				break
			poem += data.decode('utf8')
		print(poem)
		sock.close()

下面用非同步的client來讀取,程式碼如下:

import datetime, errno, optparse, select, socket

def connect(port):
	"""Connect to the given server and return a non-blocking socket."""
	address = ('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 = list(map(connect, ports))
	poems = dict.fromkeys(sockets, '') # socket -> accumulated poem
	sock2task = dict([(s, i + 1) for i, s in enumerate(sockets)])

	while sockets:
		#運用select來確保那些可讀取的非同步socket可以立即開始讀取IO
		#OS不停的搜尋目前可以read的socket,有的話就返回rlist
		rlist, _, _ = select.select(sockets, [], [])
		for sock in rlist:
			data = ''
			while True:
				try:
					new_data = sock.recv(1024)
					new_data = new_data.decode('utf8')
				except socket.error as 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:
				print(poems[sock])	# 列印sock接收到的資料
				sockets.remove(sock)
				sock.close()
				print('Task %d finished\n' % task_num)
			else:
				addr_fmt = format_address(sock.getpeername())
				msg = 'Task %d: got %d bytes of poetry from %s\n'
				print(msg % (task_num, len(data), addr_fmt))

			poems[sock] += data		# 儲存每個sock接收到的資料

	elapsed = datetime.datetime.now() - start
	print('Got poems in %s' %elapsed)

結果只需要2秒就完成了讀取任務。效率非常明顯啊。對客戶端的非同步改造主要有兩點: 


    • 同步模式下,客戶端分別建立socket;而在非同步模式下,client開始就建立了所有的socket。
    • 通過“sock.setblocking(0)”設定socket為非同步模式。
    • 通過Unix系統的select來返回可讀取socket資訊
    • 最為核心的是8行和26行。尤其是26行的select操作返回待讀取socket的列表。


相關文章