我想封装python的功能websockets封装成一个类,代表一个传感器协调器。这样做的目的是允许我创建一个协调器对象,并且只让服务器在需要时持续存在。不幸的是,我无法在网上找到任何类似的例子,并且到目前为止一直在努力。
我的代码如下:
import asyncio
import json
import logging
import websockets
logging.basicConfig()
class Coordinator(object):
def __init__(self, host='localhost', port=8080):
self.host = host
self.port = port
self.running = False
self.server = None
self.sensors = set()
def __enter__(self):
self.server = websockets.serve((self.ws_handler, self.host, self.port))
self.running = True
def __exit__(self, exc_type, exc_val, exc_tb):
# Gracefully stop serving
self.running = False
pass
def sensors_event(self):
return json.dumps({'type': 'sensors', 'count': len(self.sensors)})
async def notify_sensors(self):
if self.sensors:
message = self.sensors_event()
await asyncio.wait([user.send(message) for user in self.sensors])
async def register(self, websocket):
self.sensors.add(websocket)
await self.notify_sensors()
async def unregister(self, websocket):
self.sensors.remove(websocket)
await self.notify_sensors()
async def ws_handler(self, websocket):
try:
await self.register(websocket)
pass
finally:
await self.unregister(websocket)
if __name__ == '__main__':
with Coordinator() as coordinator:
pass
目前 websocket 服务器似乎没有启动,因为它在 netstat 上不可见。
是否可以在由协调器对象持有的单独(恶魔化)线程中运行服务器?
谢谢
最佳答案
The
websockets.server
module defines a simpleWebSocket
server API.
serve()
returns an awaitable. Awaiting it yields an instance ofWebSocketServer
which providesclose()
andwait_closed()
methods for terminating the server and cleaning up its resources.On Python ≥ 3.5,
serve()
can also be used as an asynchronous context manager. In this case, the server is shut down when exiting the context.
正如@user4815162342 已经确定的那样,主要问题是您没有等待对 serve()
协程的调用。
由于您使用的是 Python v3.6.8,因此您可以使用异步上下文管理器来简化实现。这样做的好处是您无需担心处理关机,因为它是自动处理的。这是一个简单回显服务器的面向对象实现。
import asyncio
import signal
import websockets
class Server(object):
def __init__(self, host, port):
self.host, self.port = host, port
self.loop = asyncio.get_event_loop()
self.stop = self.loop.create_future()
self.loop.add_signal_handler(signal.SIGINT, self.stop.set_result, None)
self.loop.run_until_complete(self.server())
async def server(self):
async with websockets.serve(self.ws_handler, self.host, self.port):
await self.stop
async def ws_handler(self, websocket, path):
msg = await websocket.recv()
print(f'Received: {msg}')
await websocket.send(msg)
print(f'Sending: {msg}')
if __name__ == '__main__':
server = Server(host='localhost', port=6789)
目前,这将一直运行到用户发送中断为止,但您可以调整 stop
future 以适应。
关于python - OOP Python 网络套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54467332/