Skip to content

The fast API receives both websocket and HTTP requests. Websocket will delay HTTP requests. What should I do? #2580

@lemonit-eric-mao

Description

@lemonit-eric-mao

python3.6

fastapi 0.63.0

server

#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Time    : 2020/11/25 11:28
# @Author  : Eric.Mao
# @FileName: main.py
# @Software: PyCharm
# @Blog    : http://www.dev-share.top/

import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
    allow_origins=["*"],
)

if __name__ == '__main__':
    uvicorn.run('main:app', host='localhost', port=8000, reload=True, debug=True, log_level="debug")

'''
========================================================================================
'''

import time
from fastapi import WebSocket, WebSocketDisconnect


# websocket test
@app.websocket("/websocket")
async def mapping_exec(websocket: WebSocket):
    await websocket.accept()
    try:
        while True:
            receive_text = await websocket.receive_text()
            print(receive_text)
            time.sleep(30)
    except WebSocketDisconnect:
        print('客户端重新链接!')


# http test
@app.get("/mapping/{page_no}/{page_size}/{mapping_name}", tags=["mapping"])
def query_mapping_all(page_no: int, page_size: int, mapping_name: str):
    print('请求成功')

Implementation steps:

  1. The client sends information to websocket and makes it wait for 30 seconds at the server

  2. The client is sending the HTTP request. At this time, the HTTP request will be in pending, and it will not continue to process until the websocket wait ends for 30 seconds

I don't know why it's blocked. I'll ask experts to give me more advice


200 20
200 20

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions