基于gevent的靠UDP通信。
项目名称来自我的老家"西王舍村", 正式名称待定。
- HTTP
- 优点
- 通用性好,平台,语言支持广泛,被认知度高。
- 支持Request-response, 使用方便,直接能拿到请求处理结果。
- 缺点
- 好多HTTP的Server和Client不支持HTTP Persistent Connection。
- 好多HTTP的Server和Client不支持HTTP Pipeling。
- 不支持Full-duplex,服务端不能直接给客户端发通知。
- Response和Request都不支持sequence number,导致Server和Client不能纯异步的处理消息。
- 优点
- WebSocket:
- 优点
- 支持持久连接,纯异步收发消息
- 有on_error,on_close等事件,方便处理连接断开等事件
- 缺点
- 不支持Request-response,需要自己匹配应答。
- 优点
- ZeroMQ
- 优点
- 覆盖多种通信模式,request/response, sub/pub等
- 使用简单,性能好
- 缺点
- 功能太多,不够轻量级,据说高并发下有丢失消息的问题。
- 内部实现理解复杂,不利于将来排错。
- 优点
- xiwangshe
- 优点
- 全双工,客户端和服务端都可以主动给对方发送request
- 每个message都有唯一的sequence number,客户端和服务端均可纯异步的处理消息
- client可以同时发起多个request,无需等待之前的request的response
- server收到response后可以在任何时候异步的返回response
- 得益于gevent,IO性能很好
- 得益于gevent, 收发消息都是同步的写法,异步的性能,不需要很多Callback,更直观
- 底层使用UDP协议,不需要进行连接管理,不需要处理连接异常断开等事件
- 代码简单,只有200多行代码,出错很容易排查故障
- 自带完善的单元测试,性能测试,性能剖析脚本
- 序列化组件默认使用msgpack,如果没有安装该模块会自动降级为SimpleJson或json
- 缺点
- 因为是UDP协议,所以不适合传输大数据,保险的话最好传输512字节以下的包
- 优点
Server
from xiwangshe import Server
class TestServer(Server):
def __init__(self, url):
Server.__init__(self, url)
def on_request(self, request):
if request.method == 'hi':
request.send_response(200, request.body)
return
server = TestServer(('localhost', 4446))
server.start()
Client
from xiwangshe import client
from xiwangshe import TimeoutException
try:
response = client.send_request(url=('localhost', 4446),
method='hi',
body='onlytiancai',
timeout=5)
print response.status
print response.body
print response.request
except TimeoutException, ex:
print ex