python asyncio uvloop socket server
来源:互联网 发布:哈登什么数据是第一 编辑:程序博客网 时间:2024/05/22 22:41
用asyncio搭建高并发的socket server,如果使用python3.5,则使用uvloop,性能将更好
import asyncio
import sysclients = []class SimpleChatClientProtocol(asyncio.Protocol): def connection_made(self, transport): self.transport = transport self.peername = transport.get_extra_info("peername") print("connection_made: {}".format(self.peername)) clients.append(self) def data_received(self, data): print("data_received: {}".format(data.decode())) for client in clients: if client is not self: client.transport.write("{}: {}".format(self.peername, data.decode()).encode()) def connection_lost(self, ex): print("connection_lost: {}".format(self.peername)) clients.remove(self)if __name__ == '__main__': print("starting up..") PY35 = sys.version_info >= (3, 5) if PY35: try: import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) print('using uvloop') except: print('using asyncio default loop') else: print('using asyncio default loop') loop = asyncio.get_event_loop() coro = loop.create_server(SimpleChatClientProtocol, port=8000) server = loop.run_until_complete(coro) for socket in server.sockets: print("serving on {}".format(socket.getsockname())) loop.run_forever()
阅读全文
1 0
- python asyncio uvloop socket server
- python asyncio
- Python asyncio文档翻译--asyncio.Future
- 【asyncio】python异步IO
- Python标准模块--asyncio
- Python asyncio文档翻译
- python asyncio学习记录
- Python -asyncio笔记
- 【Python】asyncio异步爬虫
- python asyncio模块
- Python asyncio使用范例
- http client/server for asyncio
- uvloop: 快得发烫的Python网络库
- python学习——asyncio
- asyncio
- asyncio
- asyncio
- python socket-server-client
- python 中zip()函数
- 编写一个表达学生和课程关系的程序-Java描述
- git忽略已经被提交的文件
- chmod 777 修改权限
- linux fread 的返回值问题
- python asyncio uvloop socket server
- js随机颜色生成
- Ubuntu14.0搭建Apache+php+mysql环境(腾讯云)
- jauery+ajax+amazeUi分页
- hpuoj【1290】奇数次元素【统计元素】
- 2017多校第七场02(思维题目)
- Oracle数据库结构化查询 Start with connect by prior使用
- JDK1.8源码分析之HashMap
- JavaEE框架面试常问问题