-1

server.py

from fastapi import FastAPI

import uvicorn
import time

app = FastAPI()

@app.post('/upload')
async def upload(file: dict):
    print('hello')

    time.sleep(1)
    
    return file

if __name__ == '__main__':
    uvicorn.run(app, host='', port=1024)

client.py

import requests
import threading

def thread_main(a):
    response = requests.post(url, json={'0':0, '1':1, '2':2})
    if response.status_code == 200:
        print(a, response.json())
    else:
        print(a, 'wrong')

url = 'http://IP:1024/upload'
for i in range(10):
    thread = threading.Thread(target=thread_main, args=(i,))
    thread.start()

server logs are ''' hello INFO: IP:port - "POST /upload HTTP/1.1" 200 OK hello INFO: IP:port - "POST /upload HTTP/1.1" 200 OK hello INFO: IP:port - "POST /upload HTTP/1.1" 200 OK ... '''

but i want them to be ''' hello hello hello ... INFO: IP:port - "POST /upload HTTP/1.1" 200 OK INFO: IP:port - "POST /upload HTTP/1.1" 200 OK INFO: IP:port - "POST /upload HTTP/1.1" 200 OK ... '''

is it possible in fastapi?

i thought that using 'async' means server process requests asynchronously but it doesn't

additionally how do I know if the function is a blocking function? and how do i find an alternative function?

hanover
  • 1
  • 2

1 Answers1

0

You are using synchronous(blocking) time.sleep(1) inside async function.

Blocking functions block the thread completely, which is why other operations on this thread cannot be executed

In this case, the execution of the main thread is blocked until the end of time.sleep, and fastapi cannot process new requests while the thread is blocked

In this particular case, you need to use asynchronous delay, aka asyncio.sleep

server.py

from fastapi import FastAPI

import uvicorn
import time
import asyncio

app = FastAPI()

@app.post('/upload')
async def upload(file: dict):
    print('hello')

    await asyncio.sleep(1)
    
    return file

if __name__ == '__main__':
    uvicorn.run(app, host='', port=1024)
kin4stat
  • 192
  • 1
  • 11