Skip to content

Commit

Permalink
limit the no.of threads according to cpu core
Browse files Browse the repository at this point in the history
  • Loading branch information
nrjadkry committed Feb 16, 2024
1 parent d1ac525 commit 0015b7e
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import shapely.wkb as wkblib
import sozipfile.sozipfile as zipfile
from asgiref.sync import async_to_sync
from cpuinfo import get_cpu_info
from fastapi import File, HTTPException, Response, UploadFile
from fastapi.concurrency import run_in_threadpool
from fmtm_splitter.splitter import split_by_sql, split_by_square
Expand Down Expand Up @@ -1283,8 +1284,13 @@ def wrap_generate_task_files(task):
except Exception as e:
log.exception(str(e))

# Use a ThreadPoolExecutor to run the synchronous code in threads
with ThreadPoolExecutor() as executor:
# The number of threads is based on the CPU cores
info = get_cpu_info()
cores = info["count"] * 2

# Use a ThreadPoolExecutor to run the synchronous code in threads.
# Used no.of threads = number of cores - 2
with ThreadPoolExecutor(max_workers=cores - 2) as executor:
# Submit tasks to the thread pool
futures = [
executor.submit(wrap_generate_task_files, task) for task in task_list
Expand Down

0 comments on commit 0015b7e

Please sign in to comment.