为什么我的websocket服务器仅接受1个连接

时间:2019-06-14 15:17:12

标签: python websocket python-asyncio

我正在构建一个Websocket服务器(python3.7,websockets),该服务器使用Kafka消息(python-kafka),然后将消息发送到websocket客户端。我的代码如下。它仅接受第一个连接。为什么?

import asyncio
from kafka import KafkaConsumer
import websockets
kafka_consumer = KafkaConsumer(
    'notifications',
    bootstrap_servers=[server],
    auto_offset_reset='earliest',
    enable_auto_commit=True,
    group_id='group2',
    value_deserializer=lambda x: loads(x.decode('utf-8')))

@asyncio.coroutine
async def notification(websocket, path):
    # register(websocket) sends user_event() to websocket
    for msg in kafka_consumer:
        await websocket.send(json.dumps(msg.value))

asyncio.get_event_loop().run_until_complete(
    websockets.serve(notification, '0.0.0.0', 8081))
asyncio.get_event_loop().run_forever()

0 个答案:

没有答案