Skip to content

Commit 7a4ac70

Browse files
authored
Call __init__() from request thread pool (#1146)
1 parent c4b1994 commit 7a4ac70

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

pkg/workloads/cortex/serve/serve.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@
5151
API_LIVENESS_UPDATE_PERIOD = 5 # seconds
5252

5353

54+
request_thread_pool = ThreadPoolExecutor(max_workers=int(os.environ["CORTEX_THREADS_PER_PROCESS"]))
5455
loop = asyncio.get_event_loop()
55-
loop.set_default_executor(
56-
ThreadPoolExecutor(max_workers=int(os.environ["CORTEX_THREADS_PER_PROCESS"]))
57-
)
56+
loop.set_default_executor(request_thread_pool)
5857

5958
app = FastAPI()
6059

@@ -239,7 +238,15 @@ def get_summary():
239238
return response
240239

241240

241+
# this exists so that the user's __init__() can be executed by the request thread pool, which helps
242+
# to avoid errors that occur when the user's __init__() function must be called by the same thread
243+
# which executes predict(). This only avoids errors if threads_per_worker == 1
242244
def start():
245+
future = request_thread_pool.submit(start_fn)
246+
return future.result()
247+
248+
249+
def start_fn():
243250
cache_dir = os.environ["CORTEX_CACHE_DIR"]
244251
provider = os.environ["CORTEX_PROVIDER"]
245252
spec_path = os.environ["CORTEX_API_SPEC"]

0 commit comments

Comments
 (0)