← Files Dataiku HeadlessARCHIVED FILE

dataiku_mcp/executors.py

1.66 KB · Nov 27, 2026 · 19:32 UTC

↓ Download file

# Copyright 2026 Dataiku SAS
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Shared executors for blocking Dataiku and Cobuild operations."""

import asyncio
import contextvars
import os
from concurrent.futures import ThreadPoolExecutor

_executor = ThreadPoolExecutor(
    max_workers=int(os.environ.get("DKU_MCP_MAX_WORKERS", "4"))
)
# Bounds parallel blocking Cobuild calls; additional retained turns queue locally.
_cobuild_executor = ThreadPoolExecutor(
    max_workers=int(os.environ.get("DKU_MCP_MAX_COBUILD_WORKERS", "4"))
)


async def run_blocking(func, *args, **kwargs):
    """Run a blocking function in the thread pool executor."""
    loop = asyncio.get_event_loop()
    context = contextvars.copy_context()
    return await loop.run_in_executor(
        _executor,
        context.run,
        lambda: func(*args, **kwargs),
    )


async def run_cobuild_blocking(func, *args, **kwargs):
    """Run a blocking Cobuild SDK call without occupying the general executor."""
    loop = asyncio.get_event_loop()
    context = contextvars.copy_context()
    return await loop.run_in_executor(
        _cobuild_executor,
        context.run,
        lambda: func(*args, **kwargs),
    )

SHA-256: 3049b2c090e314915215841c51b2181e00e4cadad486288ddd499a006ce72bc5