gpt4 book ai didi

python - FastAPI WebSocket无法处理大量数据输入?

转载 作者:行者123 更新时间:2023-12-02 19:14:44 27 4
gpt4 key购买 nike

我已经使用python和FastAPI创建了一个websocket服务器:https://fastapi.tiangolo.com/
并且..结束了,当然最后,我发现了一个新的“错误”,这让我有些 panic 。
当连接的客户端仅发送大量数据时,他突然断开连接。我不确定是websocket代码本身,是asyncio还是uvicorn,docker还是不能处理大量数据传入的linux服务器本身?
这是我在dockercontainer中运行的服务器代码:

import json
import time

import socketio

from fastapi import FastAPI, WebSocket
from fastapi.responses import HTMLResponse

from starlette.websockets import WebSocketDisconnect


from typing import List


app = FastAPI()


sio = socketio.AsyncServer(async_mode='asgi')
socket_app = socketio.ASGIApp(sio, static_files={'/': 'app.html'})
background_task_started = False


from streaming_class import repetisjons_klasse


import asyncio
from pydantic import BaseModel

class UserClientWebSocket(BaseModel):
id: str
ws: WebSocket

class Config:
arbitrary_types_allowed = True


class WebPageClientWebSocket(BaseModel):
id: str
ws: WebSocket

class Config:
arbitrary_types_allowed = True

class ConnectionManager:
def __init__(self):

self.active_user_client_connections: List[UserClientWebSocket] = []
self.collect_user_IDs = []


self.active_webpage_client_connections: List[WebPageClientWebSocket] = []
self.collect_webpages_IDs = []


async def connect_the_user_client(self, websocket: WebSocket, THE_USER_ID):
await websocket.accept()
await self.send_message_to_absolutely_everybody(f"User: {THE_USER_ID} connected to server!")
print("User: {} ".format(THE_USER_ID) + " Connected")
if THE_USER_ID not in self.collect_user_IDs:
self.collect_user_IDs.append(THE_USER_ID)
else:
await self.send_message_to_absolutely_everybody(f"Somebody connected with the same ID as client: {THE_USER_ID}")
await self.send_message_to_absolutely_everybody("but Vlori is a nice and kind guy, so he wil not get kicked :)")
self.collect_user_IDs.append(THE_USER_ID)
self.active_user_client_connections.append(UserClientWebSocket(ws=websocket, id=THE_USER_ID)) #SJEEKK DENNE LINJA !!!!!
await self.show_number_of_clients()



async def connect_the_webpage_client(self, websocket: WebSocket, the_webpage_id):
await websocket.accept()
await self.send_message_to_absolutely_everybody(f"User: {the_webpage_id} connected to server through webbrowser!")
print("User: {} ".format(the_webpage_id) + " Connected")
if the_webpage_id not in self.collect_webpages_IDs:
self.collect_webpages_IDs.append(the_webpage_id)
else:
await self.send_message_to_absolutely_everybody(f"Somebody connected with the same ID as client: {the_webpage_id}")
await self.send_message_to_absolutely_everybody("but Vlori is a nice and kind guy, so he wil not get kicked :)")
self.collect_webpages_IDs.append(the_webpage_id)
self.active_webpage_client_connections.append(WebPageClientWebSocket(ws=websocket, id=the_webpage_id)) #SJEEKK DENNE LINJA !!!!!
await self.show_number_of_clients()

async def disconnect_the_webpage_client(self, websocket: WebSocket, the_webpage_id):
await websocket.close(code=1000)
self.collect_webpages_IDs.remove(the_webpage_id)
self.active_webpage_client_connections.remove(WebPageClientWebSocket(ws=websocket, id=the_webpage_id))
await self.show_number_of_clients()


async def disconnect_the_user_client(self, websocket: WebSocket, THE_USER_ID):
await websocket.close(code=1000)
self.collect_user_IDs.remove(THE_USER_ID)
self.active_user_client_connections.remove(UserClientWebSocket(ws=websocket, id=THE_USER_ID))
await self.show_number_of_clients()





"""
PROBLEM: THE USER GETS DISCONNECTED WHEN SENDING TO MUCH DATA IN REAL TIME!
"""

@app.websocket("/ws/testchannel")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
try:
while True:
data = await websocket.receive_text()
print("Received data: {} ".format(data))
#await websocket.send_text(f"you sent message: {data}")
¤await connection_manager.send_message_to_absolutely_everybody(data)


except WebSocketDisconnect:
print("client left chat.")
以下是我在dockerfile中运行的设置(可能在这里吗?我不确定):
FROM ubuntu:latest
FROM python:3

MAINTAINER raxor2k "xxx.com"

RUN apt-get update -y

RUN apt-get install -y python3-pip build-essential python3-dev

COPY . /app
WORKDIR /app

RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt
RUN pip3 install fastapi uvicorn #dennekanfjernes?

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80", "--reload"]
因此,这是我的client.py文件,它是从本地计算机运行的(运行良好):
#client.py

from websocket import create_connection
import json
import time

ws = create_connection("ws://134.122.76.213:5080/ws/testchannel")

time.sleep(1)


def send_json_all_the_time(position):
generate_json = { "machineID":"001", "RepSensor": position}
send_json = json.dumps(generate_json)
print("JSON SENT FROM IoT SENSOR: {}".format(send_json))
time.sleep(0.1)
ws.send(json.dumps(send_json))
time.sleep(0.1)


while True:
for x in range(1000):
send_json_all_the_time(x)

for x in range(100, -1, -1):
ws.send("pause a little bit, starting again soon!")
send_json_all_the_time(x)
所以我很困惑..我的服务器代码是不正确的吗?是linux服务器问题吗?容器内部的问题是什么?
任何帮助将不胜感激!

最佳答案

编辑:经过FastAPI“论坛”的一些研究,测试和反馈:https://gitter.im/tiangolo/fastapi
由于缺少乒乓同步,问题出在客户端上。服务器端没有错。

关于python - FastAPI WebSocket无法处理大量数据输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64099599/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com