(编辑:jimmy 日期: 2024/11/11 浏览:2)
单线程同步
消息协议
// 输入 { in: "ping", params: "ireader 0" } // 输出 { out: "pong", result: "ireader 0" }
客户端 client.py
# coding: utf-8 # client.py import json import time import struct import socket def rpc(sock, in_, params): response = json.dumps({"in": in_, "params": params}) # 请求消息体 length_prefix = struct.pack("I", len(response)) # 请求长度前缀 sock.sendall(length_prefix) sock.sendall(response) length_prefix = sock.recv(4) # 响应长度前缀 length, = struct.unpack("I", length_prefix) body = sock.recv(length) # 响应消息体 response = json.loads(body) return response["out"], response["result"] # 返回响应类型和结果 if __name__ == '__main__': s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("localhost", 8080)) for i in range(10): # 连续发送10个rpc请求 out, result = rpc(s, "ping", "ireader %d" % i) print out, result time.sleep(1) # 休眠1s,便于观察 s.close() # 关闭连接
服务端 blocking_single.py
# coding: utf8 # blocking_single.py import json import struct import socket def handle_conn(conn, addr, handlers): print addr, "comes" while True: # 循环读写 length_prefix = conn.recv(4) # 请求长度前缀 if not length_prefix: # 连接关闭了 print addr, "bye" conn.close() break # 退出循环,处理下一个连接 length, = struct.unpack("I", length_prefix) body = conn.recv(length) # 请求消息体 request = json.loads(body) in_ = request['in'] params = request['params'] print in_, params handler = handlers[in_] # 查找请求处理器 handler(conn, params) # 处理请求 def loop(sock, handlers): while True: conn, addr = sock.accept() # 接收连接 handle_conn(conn, addr, handlers) # 处理连接 def ping(conn, params): send_result(conn, "pong", params) def send_result(conn, out, result): response = json.dumps({"out": out, "result": result}) # 响应消息体 length_prefix = struct.pack("I", len(response)) # 响应长度前缀 conn.sendall(length_prefix) conn.sendall(response) if __name__ == '__main__': sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 创建一个TCP套接字 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 打开reuse addr选项 sock.bind(("localhost", 8080)) # 绑定端口 sock.listen(1) # 监听客户端连接 handlers = { # 注册请求处理器 "ping": ping } loop(sock, handlers) # 进入服务循环
多线程同步
服务端 multithread.py
多进程同步
PreForking同步
单进程异步
PreForking异步
Tornado/Nginx采用了多进程PreForking异步模型,具有良好的高并发处理能力
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。