6

In FastAPI, run_in_executor and run_in_threadpool both can let function running in other thread, and it seems to have the same behavior.

But what's difference between this ? And what's the best choice for using FastAPI?

Demo:

import asyncio
import time
from fastapi import FastAPI
from fastapi.concurrency import run_in_threadpool

app = FastAPI()


@app.get("/")
async def index():
    result = await long_time_work()
    result = await long_time_work2()
    return {"message": result}


async def long_time_work():
    loop = asyncio.get_event_loop()
    await loop.run_in_executor(None, time.sleep, 5)
    return True


async def long_time_work2():
    await run_in_threadpool(lambda: time.sleep(5))
    return True

Vic
  • 758
  • 2
  • 15
  • 31

1 Answers1

1

run_in_threadpool is using anyio.to_thread.run_sync under the hood. This allows it to work with different async backends based on the environment (asyncio or trio).

Both of them are running sync methods in a thread pool but run_in_executor gives a lot more control to the user like selecting a different executor. This will allow you to run your sync methods in a specific thread pool separated from the default one, limiting the number of concurrent threads and even passing process executor instance to run them in separate processes.

Mahdi Sadeghi
  • 673
  • 5
  • 13